jQuery - 检索输入字段的名称,然后更改它 [英] jQuery - Retrieve an Input fields' name, and then change it

查看:63
本文介绍了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?

谢谢。< br>
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

这篇关于jQuery - 检索输入字段的名称,然后更改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆