通过使用JQuery单击链接填写表中的不同输入字段

时间:2022-08-26 20:19:47

How do I get the value clicked in each row (generated using AddRowTable plugin), i.e.,

如何获取每行中单击的值(使用AddRowTable插件生成),即

<script type="text/javascript">
$("document").ready(function(){
$(".addRow-Max4").btnAddRow({maxRow:4, inputBoxAutoNumber:true});
$(".delRow").btnDelRow();
});
</script> 

generates maximal 4 rows in a table and numbers each input box, so I get textfield1, textfield2 and so onin my table.

在表格中生成最多4行并为每个输入框编号,因此我在表格中获得textfield1,textfield2等。

<table border="0" >
<tr>
    <th colspan="2">Input your data</th>
    <th><input type="button" value="Add Row" class="addRow-Max4"/></th>
</tr>
<tr><td>
    <a href="#" class="autofill">Apple</a> 
    <a href="#" class="autofill">Banana</a> 
    <a href="#" class="autofill">Mango</a>
    </td><td><input type="text" name="textfield" class="autofill" size="25"/></td>
    <td><img src="./images/cross.gif" class="delRow" border="0"></td>
</tr>
</table>

Now I might decide for Apple in row1, Mango in row2 and so on and would like that the according textfield for this row is filled.

现在我可能会在第1行中决定使用Apple,在第2行中决定使用Mango等等,并希望填充此行的相应文本字段。

I have

$("a[class=autofill]").live("click", function(){
$("input[name=textfield1]").val($(this).text());
return false;
});

but how do I have to adjust it to make it dynamic for each row. Let's say textfield1, textfield2, textfield3 and so on. As it is right now, I change the textfield1 each time a link is clicked. But I want to just change it if one of the corresponding (same row) link is clicked.

但我如何调整它以使其为每一行动态。我们说textfield1,textfield2,textfield3等等。就像现在一样,每次点击链接时我都会更改textfield1。但是,如果单击其中一个相应的(同一行)链接,我想更改它。

1 个解决方案

#1


1  

Did not get much of the insight for this issue. But here is my try to find all the input fields inside their parent TR.

没有得到很多关于这个问题的见解。但这是我尝试在其父TR中找到所有输入字段。

$("a[class=autofill]").live("click", function(){
    $(this).closest('tr').find("input").val($(this).text());
    return false;
});

This is what i tried:

这是我试过的:

http://jsfiddle.net/KU6Xw/

#1


1  

Did not get much of the insight for this issue. But here is my try to find all the input fields inside their parent TR.

没有得到很多关于这个问题的见解。但这是我尝试在其父TR中找到所有输入字段。

$("a[class=autofill]").live("click", function(){
    $(this).closest('tr').find("input").val($(this).text());
    return false;
});

This is what i tried:

这是我试过的:

http://jsfiddle.net/KU6Xw/