如何使用Jquery设置输入字段的Name属性?

Tik*_*L13 31 forms jquery attributes

快速问题......我想知道如何才能实现这一目标.

好的,我有一个像这样的输入字段:

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

我想点击ID为#resetPrompt的div,并使用"other_amount"从输入中填充空名称attr

我没有想法,这是漫长的一天

多谢你们,

马特

Ale*_*ber 60

$('#resetPrompt').click(function(){
    $('#input1').attr('name', 'other_amount');
});
Run Code Online (Sandbox Code Playgroud)


eug*_*ene 5

这是给所有谷歌用户的:

应该注意的是,Alex 的提议虽然在大多数“真正的”浏览器中有效,但在 ie 8 及以下版本(jquery 1.6.2)中不起作用。在设置输入字段的“name”属性时,为了解决错误,Microsoft 在动态输入字段附加到 DOM 时添加了一个虚构的“submitName”属性。

要解决 ie 中的问题,要么强制用户升级、使用 Firefox、Chrome 等,要么需要手动添加输入字段:

$("#resetPrompt").click(function(){
   $("#input1").remove();
   $("#input_container").append('<input id="input1" name="' + other_amount + "' type="text" class="checked other_amount" onFocus="Toggle(\'radio1\', \'input1\');" />');
});
Run Code Online (Sandbox Code Playgroud)