为什么我的jQuery focusout(或blur)事件没有被触发?

3
在这个jsfiddle的底部链接中,我有以下HTML代码:
<input type="text" id="BLAboxPaymentAmount" value="2">
</br>
<input type="text" id="BLAboxSection5Total" value="3">

...和这个jQuery:

$(document).on("focusout", '[id$=boxSection5Total]', function (e) {
    var totalvalue = $(this).val();
    //alert("boxSection5Total val is " + totalvalue);
    var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
    //alert("Payment Total val is " + paymenttotalvalue);
    if (totalvalue !== paymenttotalvalue) {
        alert("The value in 'Total' does not equal the previous value in 'Payment Total'");
    } else {
        alert("values ARE equal");
    }
});

在jsfiddle中它能够很好地工作,但是在我的Sharepoint页面中事件没有触发 - 我在boxPaymentAmount(“5”)中输入一个值,在boxSection5Total中再次输入另一个值(“6”),切换到boxSection5Total后,我没有看到警报,而根据这个jQuery,我应该看到警报,因为它与jsfiddle中的代码几乎完全相同:

$(document).on("focusout", '[id$=boxSection5Total]', function (e) {
    alert("focusout event has fired");
    var totalvalue = $(this).val();
    var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
    if (totalvalue !== paymenttotalvalue) {
        alert("The value in 'Total' does not equal the previous value in 'Payment Total'");
    }
});

所以事件甚至没有被触发,为什么呢?

我在Chrome和F12d中运行了该页面,查看控制台中的错误消息,但是没有发现任何错误。

更新

针对A.(不是"The")Wolff:

这是我在代码后端创建元素的方式:

boxPaymentAmount = new TextBox
{
    CssClass = "dplatypus-webform-field-input"
};

我还在jsfiddle中的HTML将“textarea”更改为“text”。

更新2

对于Jonathen(而不是Russell)Crowe:
我将我的jQuery更改为以下内容:
$(document).on("blur", "[id$=boxSection5Total]", function (e) {
    alert("blur event has fired");
    console.log("blur event has fired");
    var totalvalue = $(this).val();
    var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
    if (totalvalue !== paymenttotalvalue) {
        alert("The value in 'Total' does not equal the previous value in 'Payment Total'");
        console.log("The value in 'Total' does NOT equal the previous value in 'Payment Total'");
    }
    else {
        console.log("The value in 'Total' DOES equal the previous value in 'Payment Total'");
    }
});

...但是从这个事件处理程序中仍然看不到控制台中的任何内容。

更新3

我有许多其他函数/处理程序可以很好地触发,那么为什么这个函数不行是个难题。也许如果我展示一下我所拥有的东西(省略了一些肮脏的细节),它可能会阐明发生了什么(失败的函数是最后/在底部):

$(document).ready(function () {
    console.log('The ready function has been reached; Requester and Payee Status sections should be slid up'); /* This is a "sanity check" so it can be verified that this jQuery script is running/ran */
});

/* NOTE: Things that need to take place after all the elements have been constructed need to go here; the ready function can be too early */
$(window).load(function () {
    $('[id$=_AddressRows]').slideUp();
    $('[id$=panelSection2]').slideUp();
    $('[id$=panelSection3]').slideUp();

    $('[id$=ddlPayToVendor]').hide();
});

/* NOTE: this checkbox is only visible if they are not already authenticated; If they select "Yes" (self-identify as UCSC Faculty, Staff, or Student), prompt them to log in */
$(document).on("change", '[id$=ckbxUCSCFacultyStaffOrStudent]', function () {
    var ckd = this.checked;
    . . . code elided for brevity
});

/* If user selects "payment for self" (they are seeking payment for themselves, as opposed to someone else), omit (invisibilize) sections 2 and 3 on the form TODO: Should I change these from "change" to "click" */
$(document).on("click", '[id$=rbPaymentForSelf]', function () {
    if (this.checked) {
    . . . code elided for brevity
    }
});

/* If user selects "payment for someone else" (they are seeking payment for someone else, as opposed to themselves), make sections 2 and 3 on the form visible */
$(document).on("click", '[id$=rbPaymentForSomeoneElse]', function () {
    if (this.checked) {
    . . . code elided for brevity

    }
});

$(document).on("click", '[id$=rbPaymentToIndividual]', function () {
    if (this.checked) {
        $('[id$=ddlPayToVendor]').slideUp();
        $('[id$=ddlPayToIndividual]').slideDown();
    }
});

$(document).on("click", '[id$=rbPaymentToVendor]', function () {
    if (this.checked) {
        $('[id$=ddlPayToIndividual]').slideUp();
        $('[id$=ddlPayToVendor]').slideDown();
    }
});

// Refactor this to populate the elements below (description, account codes, tax 1099); may pull from Lists rather than use the hardcoded vals, but if need to do the latter, see http://jsfiddle.net/clayshannon/x8npcpss/3/
$(document).on("change", '[id$=ddlPayToIndividual]', function () {
    var value = $(this).val();
    . . . code elided for brevity

});

// Refactor this ... (see comment above)
$(document).on("change", '[id$=ddlPayToVendor]', function () {
    var value = $(this).val();
    . . . code elided for brevity

});

/* Disallow anything other than 0..9 in the "SSN or ITIN" textbox */
$(document).on("keypress", '[id$=txtbxSSNOrITIN]', function (e) {
    var k = e.which;
    if (k < 48 || k > 57) { e.preventDefault(); }
});

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    var textboxHeight = 15;
    . . . code elided for brevity

});

$(document).on("keyup", "[id$=explainPaymentTextBox]", function (e) {
    console.log("explainPaymentTextBox keyup reached");
    while ($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) {
        $(this).height($(this).height() + 1);
    };
});

HERE IS THE RECALCITRANT ONE (DOESN'T FIRE):
/* Verify that amount in "Total" equals amount in Section 1 "Payment Amount"; this is not working at present; I first tried "focusout" instead of "blur" but neither event fires... */
$(document).on("blur", "[id$=boxSection5Total]", function (e) {
    alert("boxSection5Total's blur event has fired");
    console.log("boxSection5Total's blur event has fired");
    var totalvalue = $(this).val();
    var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
    if (totalvalue !== paymenttotalvalue) {
        alert("The value in 'Total' does not equal the previous value in 'Payment Total'");
        console.log("The value in 'Total' does NOT equal the previous value in 'Payment Total'");
    }
    else {
        console.log("The value in 'Total' DOES equal the previous value in 'Payment Total'");
    }
});

1
@B. Clay Shannon:在你的代码中插入一个断点或警报,以查看在挂钩focusout事件之前$('[id$=boxSection5Total]').length的值是多少。如果该值为0,则意味着用于挂钩事件的选择器不正确。 - The_Black_Smurf
1
忘记我所说的有关 JQuery 如何评估 .on 的内容吧。根据这个答案,选择器将每次被重新评估。 - The_Black_Smurf
1
是的 - 对于你的两个问题都是。 :) - Dave Salomon
1
@B.Clay 那么唯一可能的解释是选择器没有找到该元素。将此粘贴到控制台中会给您什么?$('[id$=boxSection5Total]')Fiddle - Dave Salomon
1
我找到了问题 - 我添加了一个解释它的答案。提示:请参见上面的更新(第一个)。 - B. Clay Shannon-B. Crow Raven
显示剩余16条评论
2个回答

1

你试过使用 blur 而不是使用 focusout 吗?这两个事件之间的区别可以在这里找到,而在你的情况下,你只想捕获文本区域的事件,因为它里面没有任何项目。


将其从“focusout”更改为“blur”没有任何区别。我在必应上搜索了“什么jquery事件等同于onexit”,找到了“focusout”,这就是我首先使用它的原因。 - B. Clay Shannon-B. Crow Raven

1
这是一个“新手错误” - 我忘记给元素分配ID。一旦我将代码从这个改变:
boxSection5Total = new TextBox()
{
    CssClass = "dplatypus-webform-field-input"
};
this.Controls.Add(boxSection5Total);

...变为这样:

...

boxSection5Total = new TextBox()
{
    CssClass = "dplatypus-webform-field-input",
    ID = "boxSection5Total"
};
this.Controls.Add(boxSection5Total);

我看到了有关进入事件的警报。

因此,UPDATE 显示了错误的代码,尽管只有习惯于在 C# 中动态创建 html 元素的人才能发现它。

为了“完全披露”,这里是当前的 jQuery 代码(按设计工作):

$(document).on("blur", "[id$=boxSection5Total]", function (e) {
    var totalvalue = $(this).val();
    var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
    if (totalvalue !== paymenttotalvalue) {
        console.log("The value in 'Total' does not equal the previous value in 'Payment Total'");
        alert("The value in 'Total' does NOT equal the previous value in 'Payment Total'");
    }
    else {
        console.log("The value in 'Total' DOES equal the previous value in 'Payment Total'");
    }
});

当我在F12工具中检查元素时,我想起了这个问题。
<input name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl153" type="text" class="dplatypus-webform-field-input">

(没有ID)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接