jQuery - 检索输入字段的名称,然后更改它
问题描述:
我需要获取一行中每个输入字段的名称,然后进行更改。我很难找到引用名称字段的正确语法。例如:
I need to get the name of each input field within a row, and change it. I am having difficulty in finding the proper syntax to reference the name field. For example:
<tr id="row_0" class="dataRow">
<input type="text" class="tabcell" name="_0-3" size="6" value=7.0 />
<input type="text" class="tabcell" name="_0-7" size="6" value=7.6 />
我迭代行并想重命名每个td字段:
I iterate over the rows and want to rename each td field:
var namePrefix = "AFS";
$('td:eq(0)', this).each(function(e) {
$('td:eq(0) input[name]', this).replaceWith($(namePrefix + 'td:eq(0) input', this).val());
.... etc ...
但这不起作用。
最终结果应如下所示:
But this does not work. The end result should look like this:
<tr id="row_0" class="dataRow">
<input type="text" class="tabcell" name="AFS_0-3" size="6" value=7.0 />
<input type="text" class="tabcell" name="AFS_0-7" size="6" value=7.6/>
任何人都知道我如何引用输入字段名称并进行更改?
Anyone know how I can reference the input field name and change it?
谢谢。
Vic
Thanks.
Vic
答
使用 .attr
和 .removeAttr
更改名称的方法。
use the .attr
and .removeAttr
methods to change the name.
var elem = $('.selector'); //cache element
var name = elem.attr('name'); // accesses name attribute
elem.attr('name', myName); // changes/adds name
elem.removeAttr('name'); // removes name