JQuery keyup事件没有触发 [英] JQuery keyup event not triggering

查看:766
本文介绍了JQuery keyup事件没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a keyup event for 3 fields so that when any one of these 3 fields is typed in it triggers a function. I'm working in IE since this is the only browser our application officially supports. This is the important bits of the html It will be using.

<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。

And here is the JQuery I currently have.

$("#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");
    }
}

为什么这不能正常工作?

Why isn't this working correctly?

推荐答案

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

It looks working for me in IE 8 and Google Chrome. When do you execute those script lines? DOM may not be in ready state when you are trying to add event listeners. So, putting such event bindings in jquery's document.ready block is always helpful!

document.ready block example:

document.ready block example:

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

这篇关于JQuery keyup事件没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆