2017-08-29 80 views
0

我有这两个jquery函数,其中一个没有被调用。但是当我评论其中一个时,另一个被调用,反之亦然。DOM加载后Jquery函数不能与多个函数一起工作

$(function() { 
 
     $("[id*=tvAi1] input[type=checkbox]").bind("click", 
 
      function() { 
 

 

 
       var table = $(this).closest("table"); 
 

 
       if (table.next().length > 0 && table.next()[0].tagName == "DIV") { 
 
        //Is Parent CheckBox 
 
        var childDiv = table.next(); 
 
        var isChecked = $(this).is(":checked"); 
 
        if (isChecked) { 
 
         if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') { 
 
          bootbox.alert(
 
           "Please enter the Cell Number because you have asked for the MobileBankingService."); 
 
          this.checked = false; 
 
          $('#CellNumberTextBox').focus(); 
 
          return false; 
 
         } 
 
        } 
 
        $("input[type=radio]", childDiv).each(function() { 
 
         if (isChecked) { 
 

 
          $(this).attr("checked", "checked"); 
 
          return false; 
 
         } else { 
 
          $(this).removeAttr("checked"); 
 
         } 
 

 
        }); 
 

 
       } 
 
      }); 
 

 

 
     $("#SaveButton").bind("click", 
 
      function (e) { 
 
       
 
       $("#hdMobile").val(""); 
 
       
 
       var tv = document.getElementById("<%= tvAi1.ClientID %>"); 
 
       var chkArray = tv.getElementsByTagName("input"); 
 
       
 
       for (i = 0; i <= chkArray.length - 1; i++) { 
 

 
        if (i == 0) { 
 
         $.ajax({ 
 
          type: "POST", 
 
          url: "AddNewCustomer.aspx/SetSession", 
 
          data: {}, 
 
          contentType: "application/json; charset=utf-8", 
 
          dataType: "json", 
 
          success: function() { 
 

 
          } 
 
         }); 
 
        } 
 

 
        if (chkArray[i].type == 'radio') { 
 

 
         if (chkArray[i].checked == true) { 
 
          if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') { 
 
           bootbox.alert(
 
            "Please enter the Cell Number because you have asked for the MobileBankingService."); 
 
           $('#CellNumberTextBox').focus(); 
 
           $.hideprogress(); 
 
           return false; 
 
          } 
 

 

 
          if ($("#hdMobile").val() == "" || $("#hdMobile").val() == null) { 
 
           $("#hdMobile").val(chkArray[i].value); 
 
          } else { 
 
           $("#hdMobile").val($("#hdMobile").val() + "," + chkArray[i].value); 
 
          } 
 
         } 
 
        } 
 
       } 
 

 
      }); 
 
    });

第二届一个

$(function() { 
 
      $("[id*=tvAi] input[type=checkbox]").bind("click", 
 
       function() { 
 

 

 
        var table = $(this).closest("table"); 
 

 
        if (table.next().length > 0 && table.next()[0].tagName == "DIV") { 
 
         //Is Parent CheckBox 
 
         var childDiv = table.next(); 
 
         var isChecked = $(this).is(":checked"); 
 
         if (isChecked) { 
 
          if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') { 
 
           bootbox.alert(
 
            "Please enter the Cell Number because you have asked for the SMSService."); 
 
           this.checked = false; 
 
           $('#CellNumberTextBox').focus(); 
 
           return false; 
 
          } 
 
         } 
 
         $("input[type=radio]", childDiv).each(function() { 
 
          if (isChecked) { 
 

 
           $(this).attr("checked", "checked"); 
 
           return false; 
 
          } else { 
 
           $(this).removeAttr("checked"); 
 
          } 
 

 
         }); 
 

 
        } 
 
       }); 
 

 
      $("[id*=tvAi] input[type=radio]").bind("click", 
 
       function() { 
 
        //hdSms 
 
        var parentDIV = $(this).closest("DIV"); 
 
        if ($(this).is(":checked")) { 
 

 
         if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') { 
 
          bootbox.alert("Please enter the Cell Number because you have asked for the SMSService."); 
 
          this.checked = false; 
 
          $('#CellNumberTextBox').focus(); 
 
          return false; 
 
         } 
 

 
         $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked"); 
 
        } else { 
 
         $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked"); 
 
        } 
 
       }); 
 
      $("#SaveButton").bind("click", 
 
       function(e) { 
 
        $("#hdSms").val(""); 
 
        var tv = document.getElementById("<%= tvAi.ClientID %>"); 
 
        var chkArray = tv.getElementsByTagName("input"); 
 
        for (i = 0; i <= chkArray.length - 1; i++) { 
 

 
         if (i == 0) { 
 
          $.ajax({ 
 
           type: "POST", 
 
           url: "AddNewCustomer.aspx/SetSession", 
 
           data: {}, 
 
           contentType: "application/json; charset=utf-8", 
 
           dataType: "json", 
 
           success: function() { 
 

 
           } 
 
          }); 
 
         } 
 

 
         if (chkArray[i].type == 'radio') { 
 

 
          if (chkArray[i].checked == true) { 
 
           if ($('#CellNumberTextBox').val() == null || $('#CellNumberTextBox').val() === '') { 
 
            bootbox.alert(
 
             "Please enter the Cell Number because you have asked for the SMSService."); 
 
            $('#CellNumberTextBox').focus(); 
 
            $.hideprogress(); 
 
            return false; 
 
           } 
 

 

 
           if ($("#hdSms").val() == "" || $("#hdSms").val() == null) { 
 
            $("#hdSms").val(chkArray[i].value); 
 
           } else { 
 
            $("#hdSms").val($("#hdSms").val() + "," + chkArray[i].value); 
 
           } 
 
          } 
 
         } 
 
        } 
 

 
       }); 
 
     });

我试图将它们结合起来既但没有helped.I希望他们都来被称为根据div启用/禁用从我的代码后面.Newbie here.Help赞赏。

回答

1

你可以调用你的匿名函数来设置jQuery的方法。尝试这样的2个功能:

$(function() { 
    /*Do stuff... */ 

})(); 

在您的代码中,您设置了两个匿名函数(或闭包)。这个函数永远不会调用(至少在你的例子中)。如果你把大括号放在最后(用一些可选的函数参数),它们只能被调用(在你的例子中)。

优点:因为他们让你的一些数据(词法环境)与对数据进行操作的功能关联

闭包是有用的。这与面向对象编程有明显的相似之处,对象允许我们将一些数据(对象的属性)与一个或多个方法相关联。

因此,你可以在任何地方使用闭包,通常你可能只使用一种方法使用一个对象。

请检查documentation

+0

感谢您的回应sir.i没有得到你可以请你解释一下。 – OLDMONK

+0

对不起,我编辑我的代码:) –

+0

仍然没有奏效sir.i的确像你说的那样,把花括号放在函数的最后 – OLDMONK

相关问题