2012-09-26 56 views
-2

我需要改變一個複選框的名稱屬性,當它點擊使用javascript,但林不知道如何做到這一點。這是我用盡:更改名稱的複選框onclick

$("#first").click(function() { 

$(#first).attr("name", thatName); 

alert(#first).attr("name"); 
}); 

<input type="checkbox" name="thisName" id="first">

沒有好!任何幫助表示讚賞。

編輯...我想我明白了...任何人都可以在這個解決方案提供反饋:

$(document).ready(function(){ 
    $("#first").click(function() { 
     $("#first").attr("name", "chump"); 
     alert($("#first").attr("name")); 
    }); 
}); 

它的工作原理,但我還想聽到任何人它是否可能存在問題的思考。

回答

0

你失蹤的字符串引號:

$("#first").click(function() { 

    $("#first").attr("name", thatName); 

}); 

但你並不需要做$('#first')擺在首位的this對象現在設置它:

$("#first").click(function() { 

    $(this).attr("name", thatName); 

}); 
0

您可以在函數內部用this代替#first

$("#first").click(function() { 

    // you need to first assign a value to thatName 
    $(this).attr("name", thatName); 
    alert($(this).attr("name")); 

}); 

注意:您需要將整個alert消息包裝在圓括號中。不要忘記美元符號。

0

你缺少quotes-

var thatName = "chkBox"; 
$("#first").click(function() { 
    $("#first").attr("name", thatName); 
    alert("#first").attr("name"); 
});