javascript - 无法更新 jQuery 中文本框的值

标签 javascript jquery ajax

我正在尝试更新三个不同表行中的三个文本框的值,但它仅更新一行中的一个文本框值。我正在分享我的代码,请指导我。

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/

相关文章:

javascript - ng-if 条件使用 AngularJS 中的特殊性

javascript - amcharts 柱形图上的 zoom 类别

javascript - jquery:如何忽略直接children()并深入直到匹配指定元素?

javascript - jQuery .load() 如何防止双击加载

javascript - 为什么对 php 的简单 ajax 调用会表现出这种奇怪的行为?

javascript - 基于选择的路由器

javascript - 当 .load() 准备就绪时触发事件

jquery - 仅通过变量名引用 jQuery 对象(moSTLy DOM 对象)还是通过 $ 更快?

ajax - dajaxice:将参数传递给 python 函数

php - Laravel 中通过 ajax 请求过滤数据