我正在尝试更新三个不同表行中的三个文本框的值,但它仅更新一行中的一个文本框值。我正在分享我的代码,请指导我。
HTML:
<table>
<tr>
<td>
<input class="ppp" type="text" id="ItemBuyingPrice0" value="10" style="width:55px;" />
</td>
<td>
<input type="text" id="Itembpusd0" readonly value="1.22" style="width:50px;" />
</td>
</tr>
<tr>
<td>
<input class="ppp" type="text" id="ItemBuyingPrice1" value="10" style="width:55px;" />
</td>
<td>
<input type="text" id="Itembpusd1" readonly value="1.22" style="width:50px;" />
</td>
</tr>
<tr>
<td>
<input class="ppp" type="text" id="ItemBuyingPrice2" value="10" style="width:55px;" />
</td>
<td>
<input type="text" id="Itembpusd2" readonly value="1.22" style="width:50px;" />
</td>
</tr>
</table>
jQuery:
var BindBPCustom = function (id, BPUSD) {
var errormsg = "";
var amount = $(id).val();
var country21 = $('#CurrencyValue').val();
$.ajax({
type: "GET",
url: cc,
data: { amount: amount, country: country21 },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$(BPUSD).val(data);
},
error: function (jqXHR, exception) {
}
});
}
$(".ppp").on("keydown", function () {
var cnt = 0;
var bp = $("#ItemBuyingPrice" + cnt);
var converted = $("#Itembpusd" + cnt);
cnt++;
BindBPCustom(bp, converted);
alert(cnt);
});
我想更改在 ItemBuyingPrice0,1,2 中键入的每个相应文本上的每个 Itembpusd0,1,2 值。目前,它仅更改一个文本框值,如果我输入 ItemBuyingPrice0,它会更改 Itembpusd0 值,但我也希望在其他文本框中也如此。请指导我
最佳答案
您可以在事件处理程序中使用 this
来引用该元素。您可以使用 jquery 选择器访问下一个文本框,如下所示:
$(".ppp").on("keydown", function () {
//var bp = $("#ItemBuyingPrice" + cnt); this can be replaced with $(this)
var converted = $(this).parent().next().find("input");
BindBPCustom($(this), converted);
alert(cnt);
});
关于javascript - 无法更新 jQuery 中文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25545486/