2017-08-29 30 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

相關問題