错误绑定值jQuery

时间:2014-08-27 11:32:00

标签: javascript jquery ajax

我正在尝试将值绑定到相应的testbox keydown事件上的三个不同的文本框,但它只适用于一个文本框。我正在分享我的代码,请指导我解决方案

HTML:

<table>
<tr>
    <td>
      <input type="text" id="BuyingPrice0" value="10" style="width:55px;" />
    </td>
    <td>
      <input type="text" id="bpusd0" readonly value="1.22" style="width:50px;" />
    </td>
</tr>
<tr>
    <td>
      <input type="text" id="BuyingPrice1" value="10" style="width:55px;" />
    </td>
    <td>
      <input type="text" id="bpusd1" readonly value="1.22" style="width:50px;" />
    </td>
</tr>
<tr>
    <td>
      <input type="text" id="BuyingPrice2" value="10" style="width:55px;" />
    </td>
    <td>
      <input type="text" id="bpusd2" readonly value="1.22" style="width:50px;" />
    </td>
</tr>
</table>

JQUERY:

var BindBP = 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) {
        }
    });
}

$(document).ready(function () {
    var count = 3;
    window.count = 0;
    var bp2 = $("#BuyingPrice" + window.count);
    var converted2 = $("#bpusd" + window.count);
    $(bp2).on("keydown", function () {        
        for (var i = count; i <= count - window.count; i++) {
            var bp = $("#BuyingPrice" + i);
            var converted = $("#bpusd" + i);
            BindBP(bp, converted);
        }
    });
});

我想在相应的购买价格更改值上更改bpusd复选框的值,但现在当我输入任何ID的BuyingPrice时,它只获得BuyingPrice0值。请指导我这样做的完美方式

2 个答案:

答案 0 :(得分:0)

纠正这一行

var amount = $(id).val();

用这个

var amount = $("'#" + id + "'").val();

答案 1 :(得分:0)

使用如下:

 var errormsg = "";
 var amount = id.val();

因为您已使用与var bp = $("#BuyingPrice" + i);

相同的country21

尝试

var BindBP = 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) {
            successCallBack(BPUSD,data)
        },
        error: function (jqXHR, exception) {
        }
    });
}

function successCallBack(BPUSD,data)
{
    BPUSD.val(data);
}