JQuery keyup事件没有触发

时间:2012-11-13 11:56:36

标签: jquery internet-explorer

我正在尝试为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");
    }
}

为什么这不正常?

1 个答案:

答案 0 :(得分:2)

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

document.ready block example:

$(document).ready(function(){
    // keyup event bindings should be in this block
});
相关问题