2010-02-25 96 views
27

快速的問題......我想知道如何才能讓它發生。如何使用Jquery設置輸入字段的Name屬性?

好吧,我有一個輸入字段像這樣:

<input type="text" id="input1" size="10" name="" value="" class="checked other_amount" onfocus="Toggle('radio1', 'input1');" /> 

我想點擊一個div #resetPrompt的ID,並填補了空的name屬性從輸入與「other_amount」

我的想法它是一個漫長的一天

謝謝你們,

馬特

回答

45
$('#resetPrompt').click(function(){ 
    $('#input1').attr('name', 'other_amount'); 
}); 
+1

正如前面所指出的,這贏得了」 t在MSIE7及更高版本中工作。解決方法是使用.prop(..)重寫內部行: $('#input1')。attr('name','other_amount')|| $('#input1')。prop('name','other_amount'); – esteewhy 2013-11-18 16:32:03

1

如果 「other_amount」 是另一個輸入的ID,使用attrval如下:

$("#resetPrompt").click(function(){ 
    var otherValue = $("#other_amount").val(); 
    $("#input1").attr("name", "someName"); 
} 
+0

這些所有工作的人感謝! – TikaL13 2010-02-25 22:44:31

1

假設 「other_amount」 這個名字,而不是對應的輸入中的ID。

$('#resetPrompt').click(function() { 
    $('#input1').attr('name', $('[name=other_amount]').val()); 
}); 
+0

這些所有工作的人感謝! – TikaL13 2010-02-25 22:44:14

4

這是所有的Google員工在那裏:

應當指出的是,亞歷克斯的提議,但在最「真實」的瀏覽器功能,不能在IE 8及以下(jQuery的1.6.2工作)。在設置輸入字段的「名稱」屬性時,爲了解決錯誤,Microsoft在動態輸入字段附加到DOM時添加了虛構的「submitName」屬性。

要解決的就是這個問題,無論是強制用戶升級,使用火狐,Chrome等,或者你需要手動添加輸入字段:

​​
+0

確認:無法在MSIE7中使用$ .name(..,..)設置@name屬性 - 輸入只保留舊值並且什麼都不會發生。它在更新的jQuery版本中解決嗎? – esteewhy 2013-11-18 15:26:13

相關問題