页面重定向在jquery

时间:2017-04-19 04:52:16

标签: javascript jquery

<script>
    $(document).ready(function() {

      $("#btnSubmit").live('click',function(){     
        var sum = '0';
        $("[id^=FormData_][id$=_c_data]").each(function(){

             var c_data = $(this).val();
              var required = $(this).attr("data-required");
              var label = $(this).attr("data-label");
              if(required == '1'){

                if(c_data == ""){
                    sum += '1';
                }
            }

        });

        if(sum == "0"){

        $("[id^=FormData_][id$=_c_data]").each(function(){
              var c_data = $(this).val();
            var admin = $(this).attr("data-admin");
            var form = $(this).attr("data-form");
            var component = $(this).attr("date-component");
            var unic = $(this).attr("data-unic");
             var user = $(this).attr("data-user");
              var url = "<?php echo  Yii::app()->createUrl('formdata/admin&id='.$form_id);?>";
            if(c_data == ""){

                var site_url = "<?php echo Yii::app()->createUrl('/formdata/deleteDetail' ); ?>";
                jQuery.ajax({
                    type: "POST",
                    url: site_url,
                    data: {new_value:c_data,admin:admin,form:form,component:component,unic:unic,user:user},
                    cache: false,
                    async: false,
                    success: function(response){

                    }
                });
            } else {

                var site_url = "<?php echo Yii::app()->createUrl('/formdata/updateDetailValue' ); ?>";
                jQuery.ajax({
                    type: "POST",
                    url: site_url,
                    data: {new_value:c_data,admin:admin,form:form,component:component,unic:unic,user:user},
                    cache: false,
                    async: false,
                    success: function(response){     
                    }
                });
            } 
        });
        window.location = "http://www.example.com";

        }else {
            if(sum != ""){

                bootbox.dialog({
                        message: 'Please Fill All Required Field !',
                        title: 'Alert',
                        buttons: {
                              main: {
                                label: 'OK',
                                className: 'blue'                        
                              }
                            }
                    });
                  return false;
                }  
        }
     });     
    });
</script>

在此脚本中window.location = "http://www.example.com";不管用。 但我检查警报信息它工作正常。为什么它不能在条件下工作。 每个功能完成后我需要重定向页面。 请( - ((((((((((((((

3 个答案:

答案 0 :(得分:0)

试试这个。,

window.location.href = 'http://www.google.com';

这可能适合你。

Window.location.href and Window.open () methods in JavaScript

答案 1 :(得分:0)

jQuery不是必需的,window.location.replace(url)将最好地模拟HTTP重定向。

你还想用jQuery使用这个$(location).attr('href', 'url')

答案 2 :(得分:0)

如果我的问题正确无误,您需要在ajax功能中的所有each请求完成后重定向用户。为此,您可以创建一个array来保存每个ajax请求的成功状态,并根据此array您可以执行重定向任务。

在现有代码中添加以下几个代码段:

  1. #btnSubmit click函数中(尽管如此,我建议您使用.on()委派方法)

    var ajax_succ_arr = []; // success status container
    var this_ajax_succ = false; // flag
    
  2. successajax来电的each函数中(在 if(c_data == ""){ ... jQuery.ajax({ ... success: function(response){ if(response == "1"){ this_ajax_succ = true; // set true if required response is received } } }); ajax_succ_arr.push(this_ajax_succ); // push it to the success array } else { ... jQuery.ajax({ ... success: function(response){ if(response == "1"){ this_ajax_succ = true; // set true if required response is received } } }); ajax_succ_arr.push(this_ajax_succ); // push it to the success array } 函数中)。

    each
  3. 最后你的重定向。在if(ajax_succ_arr.indexOf(false)<0){ // if all statuses are ok window.location="http://www.example.com"; } 函数结束后立即放置。

    mysql> create table match (
    -> event_id int not null,
    -> player1_id int,
    -> player2_id int,
    -> player1_score int,
    -> player2_score int,
    -> winner_id int,
    -> foreign key (event_id) references event(id)
    -> );
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual 
    that corresponds to your MySQL server version for the right syntax to use 
    near 'match (
    event_id int not null,
    player1_id int,
    player2_id int,
    player1_score int' at line 1
    
  4. 希望这有帮助。

相关问题