如何查找和确定使用相同css类的特定文本框

时间:2014-01-06 15:25:29

标签: jquery html

如何查找和确定特定文本框并使用特定值设置此值?

我有一张这样的表:

<table class="form-table" id="customFields">
    <tr valign="top">
        <td><select class="code" id="data1_1" name="data1_1"></select></td>
        <td><input type="text" class="small1" id="data2_1" name="data2_1" value="" readonly="readonly"/></td>
        <td><input type="text" class="small1" id="data3_1" name="data3_1" value="" readonly="readonly"/></td>
        <td><input type="text" class="small2" id="data4_1" name="data4_1" value="" readonly="readonly"/></td>
        <td><input type="text" class="small1" id="data5_1" name="data5_1" value="" readonly="readonly"/></td>
    </tr>
</table>

我的jQuery:

$("#customFields").on('change', '.code', function() {
    var $this = $(this),
    id = $this.val();
    text = $this.find('option:selected').text();
    $this.closest("tr").find(".small1").val(id);
});

当我使用$this.closest("tr").find(".small1").val(id);时,我的所有文本框都将填充相同的值。

如何获得第一,第二,第三(不同类)和第四个文本框,以便我可以使用不同的过程结果值填充它?

3 个答案:

答案 0 :(得分:1)

您可以使用eq()使用索引选择数组中的特定元素。

var i=0;// i is the index to be selected.
$this.closest("tr").find(".small1").eq(i).val(id);// The first of the input element with class small1 is selected by eq(0).

答案 1 :(得分:0)

使用id获取元素的.attr(),因为对于每个输入,您的ID都不同:

$('.small1').attr('id');

答案 2 :(得分:0)

使用nth-child,这样就可以指定第一个,第二个和第三个

http://api.jquery.com/nth-child-selector/