2009-08-11 67 views
1

我「米試圖附加了一些jQuery來在GridView複選框,使用document.ready存在:爲什麼在jQuery的空元素,但通過document.getElementById()

$(document).ready(function() 
    { 
     var chkBox= document.getElementById("gvTimeSheet_ctl01_chkAll1"); 
     //I can alert chkBox.id, element exists 

     var name = $("input[name='gvTimeSheet$ctl01$chkAll1']"); 
     //Here, when I alert the id, I get a null 

     var ID = $("#gvTimeSheet_ctl01_chkAll1"); 
     //Here, when I alert the id, I get a null 

     var withClass = $(".chkAll1Class"); 
     //Here, when I alert the id, I get a null 

     var withClass2 = $(".Bill1"); 
     //Here, when I alert the id, I get a null 

     //This line causes the browswer to crash and gives me the following error 
     //Microsoft JScript runtime error: 'null' is null or not an object 
     $("#gvTimeSheet_ctl01_chkAll1").click(function()   
     { 
      var checked_status = this.checked; 
      $("input[class=Bill1]").each(function() 
      { 
      this.checked = checked_status; 
      }); 

     }); 

    });*/ 

那麼,爲什麼在發現任何嘗試jquery中的對象爲null,但在同一個方法中存在於普通的javascript中?我在這裏丟失了什麼。我在這個方法的正上方有一個腳本標記中引入了jquery js文件,我似乎無法找到任何對象在其他頁面上,我可以。

+0

您是否在使用任何其他Javascript庫? – 2009-08-11 18:56:30

+0

你能發佈生成的HTML源代碼嗎?也許在http://jsbin.com/上發佈一個沙箱將有助於理解。 – 2009-08-11 18:58:15

+0

你如何提醒身份證? – womp 2009-08-11 18:58:31

回答

2

由jQuery選擇器生成的對象實際上是一個DOM對象的包裝器,所以你不會像DOM對象那樣訪問它。

如果您僅僅提醒「name.id」,從上面的第一個示例中,jQuery包裝器上不會有任何此類屬性。試着提醒你的ID如下:

alert(name.attr("id")); 
1
var ID = $("#gvTimeSheet_ctl01_chkAll1"); 

這會返回一個jQuery對象,而不是一個ID。 ID.id也是未定義的。要獲得ID,您需要:

var ID = $("#gvTimeSheet_ctl01_chkAll1").attr("id"); 
0

是否要添加該代碼的頁面已經包括Prototype JavaScript庫?

jQuery的「$」方法不會返回null,所以這不應該是一個問題:(雖然我更喜歡$('#whatever')[0].id自己)

// This line causes the browswer to crash and gives me the following error 
// Microsoft JScript runtime error: 'null' is null or not an object 
$("#gvTimeSheet_ctl01_chkAll1").click(function() { .... }); 

所有關於需要使用.attr('id')仍然站立的評論

2
var val = $("input:radio[name$='rdoselect']:checked").val();   
if (val == 1) {    
    $('[id$=divDate]').attr('disabled', true);     
}else {    
    $('[id$=divDate]').attr('disabled', false); 
} 
+0

問題從「爲什麼」開始。代碼非常適合解釋如何,但這似乎無法解釋原因。 – corsiKa 2012-11-28 17:19:48

相關問題