更改模式中多个文本框上的事件

我有一个表格,表格的每一行都有一个复选框、文本框和名称字段。以下是html


<tr>

  <td><input type="checkbox" name="visible" id="soil_row_cb" checked></td>

  <td><input type="text" name="position" style="text-align: center;" maxlength="3" size="3" value="NA"></td>

  <td>Name</td>

  <td><input type="text" name="factor" style="text-align: center;" maxlength="3" size="3" value="NA"></td>

</tr>

我想在名称 =位置的任何文本框中输入值后立即读取该值

http://img3.mukewang.com/628f1c99000177eb05280544.jpg

繁星淼淼
浏览 117回答 2
2回答

largeQ

请看一看$("input[name='position']").keyup(function() {&nbsp; var valueOfInput = $(this).val(); //value&nbsp; var indexOfTr = $(this).parents('tr').index(); //index&nbsp; console.log(valueOfInput, '-->', indexOfTr);})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="mytable">&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="visible" id="soil_row_cb" checked></td>&nbsp; &nbsp; &nbsp; <td><input type="text" name="position" style="text-align: center;" maxlength="3" size="3" value="NA" /></td>&nbsp; &nbsp; &nbsp; <td><input type="text" name="position" style="text-align: center;" maxlength="3" size="3" value="w" /></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="visible" id="soil_row_cb" checked></td>&nbsp; &nbsp; &nbsp; <td><input type="text" name="position" style="text-align: center;" maxlength="3" size="3" value="s" /></td>&nbsp; &nbsp; &nbsp; <td>Name</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>

忽然笑

您可以将名称用作数组并为输入字段创建通用类名...希望它工作...&nbsp;$('.myclassname').on("keyup",function(){&nbsp; &nbsp; &nbsp; &nbsp; var row_index = $(this).closest("tr").index();&nbsp; &nbsp; &nbsp; &nbsp; // row_index = row_index-1; // if you have tr header then enable this also...&nbsp; &nbsp; &nbsp; &nbsp; var textvalue = $("[name='position[]']").eq(row_index).val();&nbsp; &nbsp; &nbsp; &nbsp; alert(textvalue);&nbsp; &nbsp; });<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="mytable">&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="visible" id="soil_row_cb" checked></td>&nbsp; &nbsp; &nbsp; <td><input type="text" name="position[]" class="myclassname" style="text-align: center;" maxlength="3" size="3" value="NA" /></td>&nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="visible" id="soil_row_cb" checked></td>&nbsp; &nbsp; &nbsp; <td><input type="text" name="position[]" class="myclassname" style="text-align: center;" maxlength="3" size="3" value="s" /></td>&nbsp; &nbsp; &nbsp; <td>Name</td>&nbsp; &nbsp; </tr>&nbsp; </tbody></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript