1

我正在尝试为 3 个字段创建一个 keyup 事件,以便在输入这 3 个字段中的任何一个时触发一个函数。我在 IE 中工作,因为这是我们的应用程序正式支持的唯一浏览器。这是它将使用的 html 的重要部分。

<input title="Buy Price (Cannot be used in conjunction with Percentage Price)" name="BuyRate" size="3" id="BuyRate" type="text" />
<input title="Sell Price (Cannot be used in conjunction with Percentage Price)" name="SellRate" size="3" id="SellRate" type="text" />
<input title="Percentage Price (Cannot be used in conjunction with Buy/Sell Price)" name="PercentagePrice" id="PercentagePrice" type="text" id="PercentagePrice" maxlength="9" size="5" />

这是我目前拥有的 JQuery。

$("#BuyRate").keyup(function() {
alert("1");
    checkBuySellPercentage();
});

$("#SellRate").keyup(function() {
alert("2");
    checkBuySellPercentage();
});

$("#PercentagePrice").keyup(function() {
alert("3");
    checkBuySellPercentage();
});

function checkBuySellPercentage()
{
alert("4");
    var buyrate = $.trim($("#BuyRate").val());
    var sellrate = $.trim($("#SellRate").val());
    var percentageprice = $.trim($("#PercentagePrice").val());
    if (buyrate !== "" || sellrate !== "")
    {
        $("#PercentagePrice").attr("disabled", "disabled");
    }
    else if (percentageprice !== "")
    {
        $("#BuyRate").attr("disabled", "disabled");
        $("#SellRate").attr("disabled", "disabled");
    }
    else
    {
        $("#BuyRate").removeAttr("disabled");
        $("#SellRate").removeAttr("disabled");
        $("#PercentagePrice").removeAttr("disabled");
    }
}

为什么这不能正常工作?

4

1 回答 1

2

它在 IE 8 和 Google Chrome 中看起来对我有用。你什么时候执行这些脚本行?当您尝试添加事件侦听器时,DOM 可能未处于就绪状态。因此,将此类事件绑定放在jquery 的 document.ready块中总是有帮助的!

document.ready 块示例:

$(document).ready(function(){
    // keyup event bindings should be in this block
});
于 2012-11-13T12:04:33.763 回答