Javascript $table.currentRow.1stCell的语法正确
这是我的javascript:Javascript $table.currentRow.1stCell的语法正确,javascript,jquery,Javascript,Jquery,这是我的javascript: $(document).on("keypress", "#desc", function(e) { //#desc to `$table.currentRow.1stCell` $('#cd').val() = "was change"; }); 这是我的html: <table> <tr> <td> <input type="text" id="cd"> </td>
$(document).on("keypress", "#desc", function(e) { //#desc to `$table.currentRow.1stCell`
$('#cd').val() = "was change";
});
这是我的html:
<table>
<tr>
<td>
<input type="text" id="cd">
</td>
<td>
<input type="text" id="desc">
</td>
</tr>//1st row
<tr>
<td>
<input type="text" id="cd">
</td>
<td>
<input type="text" id="desc">
</td>
</tr>//2nd row
</table>
//第一排
//第二排
如您所见,我使用的是
id
,因此当我在第一行或第二行中键入时,第一行和第二行的第一个单元格都发生了更改。我想不要使用id
或class
,而是像$table.currentRow.1stCell
,但不知道正确的格式是什么,所以我只能更改当前行。求救!谢谢:)将第一个单元格的文本更改为输入内容的基本思想
$("tbody").on("change", "input", function() {
$(this) //input
.closest("tr") //finds the row associated with the input
.find("td:first") //returns the first cell
.text(this.value); //sets the text
});
由于您更改了问题的HTML:
$("tbody").on("change", "tr td:nth-child(2) input", function () {
$(this) //input
.closest("tr") //finds the row associated with the input
.find("td:first input") //returns the first cell's input
.val(this.value); //sets the value
});
您可以尝试此代码正常工作
$(document).on("keypress","#desc",function(e){ //#desc to `$table.currentRow.1stCell`
//tr parent element find first input;
$(this).parent().parent().find("input").first().val('was change');
});
});
首先你不能有这样的东西。第二件事是,您的文档上不能有多次相同的id。@MarioLopez ooops,这是一个打字错误。等等。@MarioLopez ID实际上是可以复制的(至少每个HTML5都可以),但这样做通常是不可取的。td也没有值,我不知道如何在单元格上检测按键。
$(this)。最近的(“tr”)。find(“td:first”)
先生,我试过了,但为什么输入没有了,设置为td只是因为它替换了文本。我是根据你的原始代码写的。找到输入,设置值。最后一个,先生,但我要输入重新输入!“td:first input”设置值而不是文本