jQuery .focus(在字段 X 上)导致 .blur(在字段 X 上)被调用

发布于 2024-11-07 17:52:55 字数 2417 浏览 0 评论 0原文

有谁知道为什么 jQuery .focus 函数会导致 onblur 事件被调用。我问这个问题的原因是因为在我的 JavaScript 自定义伪类中,我正在调用它。

jQuery(thisTemp.Elements.TxtSampleId).blur(Function.createDelegate(thisTemp,     
    thisTemp.PreAccessioningLoad));

但是在我对 WCF 服务的 AJAX 调用的成功 JavaScript 函数(在同一个类中)中,我有这段代码。 *.focus 行导致上面我的委托的另一个实例被调用。我可以通过注释来证明这一点。通过这条线路,我的警报被呼叫了两次。如果没有它,我的警报只会被调用一次。

PreAccessioningLoadSuccess: function(quickDataEntryObject) {

    var val = jQuery(this.Elements.TxtSampleId).val().replace(/^\s\s*/, "").replace(/\s\s*$/, "");
    var intRegex = /^\d{1,10}$/;
    if (!intRegex.test(val)) {
        jQuery(this.Elements.SampleIdAjaxValidate).html("<span style='color:red'>Sample Id must contain between 1 and 10 digits</span>");
        jQuery(this.Elements.TxtSampleId).focus();
        jQuery(this.Elements.ImageAjaxSpinner).css("visibility", "hidden");
        alert("this alert gets called twice when .focus() function called ???");
        return false;
    }
    else {
        jQuery(this.Elements.SampleIdAjaxValidate).html("");  // clear AJAX validation
    }

……

为什么会这样呢?

=========== 这是我根据请求的委托事件:

PreAccessioningLoad: function(sender) {

    if (this.Elements.TxtSampleId.value != "") {

        var service = new Acu.LIMS.UI.Web.WCFServices.Accessioning.QuickDataEntryService();
        jQuery(this.Elements.ImageAjaxSpinner).css("visibility", "visible");
        service.PreAccessioningLoad(this.Elements.TxtSampleId.value, Function.createDelegate(this, this.PreAccessioningLoadSuccess), Function.createDelegate(this, this.PreAccessioningLoadError));
    }
    return false;  //prevent page postback
},

==================================== ======= 添加我的源代码:

<script type="text/javascript">

    function Change(obj, evt) {
        if (evt.type == "focus") {
            obj.style.borderColor = "black";
            obj.style.backgroundColor = "#90EE90";  // light green on focus
        }
        else if (evt.type == "blur") {
            obj.style.borderColor = "white";
            obj.style.backgroundColor = "#7AC5CD";  // light blue on blur
        }
    }

</script>

Does anyone know why the jQuery .focus function would cause an onblur event to get called. The reason I ask is because in my JavaScript custom psuedo class, I am calling this.

jQuery(thisTemp.Elements.TxtSampleId).blur(Function.createDelegate(thisTemp,     
    thisTemp.PreAccessioningLoad));

But inside of the success JavaScript function (in this same class) for my AJAX call to my WCF service, I have this code. The *.focus line is causing another instance of my delegate above to get called. I can prove this by commenting it out. With the line, my alert gets called twice. Without it, my alert only gets called once.

PreAccessioningLoadSuccess: function(quickDataEntryObject) {

    var val = jQuery(this.Elements.TxtSampleId).val().replace(/^\s\s*/, "").replace(/\s\s*$/, "");
    var intRegex = /^\d{1,10}$/;
    if (!intRegex.test(val)) {
        jQuery(this.Elements.SampleIdAjaxValidate).html("<span style='color:red'>Sample Id must contain between 1 and 10 digits</span>");
        jQuery(this.Elements.TxtSampleId).focus();
        jQuery(this.Elements.ImageAjaxSpinner).css("visibility", "hidden");
        alert("this alert gets called twice when .focus() function called ???");
        return false;
    }
    else {
        jQuery(this.Elements.SampleIdAjaxValidate).html("");  // clear AJAX validation
    }

...

Why would that happen?

============
Here is my delegate event per the request:

PreAccessioningLoad: function(sender) {

    if (this.Elements.TxtSampleId.value != "") {

        var service = new Acu.LIMS.UI.Web.WCFServices.Accessioning.QuickDataEntryService();
        jQuery(this.Elements.ImageAjaxSpinner).css("visibility", "visible");
        service.PreAccessioningLoad(this.Elements.TxtSampleId.value, Function.createDelegate(this, this.PreAccessioningLoadSuccess), Function.createDelegate(this, this.PreAccessioningLoadError));
    }
    return false;  //prevent page postback
},

=========================================
Adding my source code:

<asp:TextBox id="TxtSampleId" runat="server" class="LIMSInputField" onfocus="Change(this, event)" onblur="Change(this, event)"/>

<script type="text/javascript">

    function Change(obj, evt) {
        if (evt.type == "focus") {
            obj.style.borderColor = "black";
            obj.style.backgroundColor = "#90EE90";  // light green on focus
        }
        else if (evt.type == "blur") {
            obj.style.borderColor = "white";
            obj.style.backgroundColor = "#7AC5CD";  // light blue on blur
        }
    }

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-11-14 17:52:55

听起来问题是警报触发了模糊(因为它现在正在接收焦点):

alert("this alert gets called twice when .focus() function called ???");

请参阅:

http:// /jsfiddle.net/on50g7cr/2/

如果您点击“焦点”按钮,然后单击“点击我!”您会看到模糊事件被触发两次(一次是因为“单击我”按钮失去焦点,然后再次是因为警报窗口失去焦点)。

Sounds like the issue is that the alert fires the blur (as it is now receiving the focus):

alert("this alert gets called twice when .focus() function called ???");

See:

http://jsfiddle.net/on50g7cr/2/

If you tab to the "focuser" button and then click "click me!" you'll see that the blur event is fired twice (once for losing focus to the "click me" button and then again for losing focus to the alert window).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文