javascript - 为什么我的 jQuery focusout(或模糊)事件没有被触发?

标签 javascript jquery html focusout jquery-focusout

在 jsfiddle 的底部 here ,我有这个 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”)沃尔夫:

这就是我在代码隐藏中创建元素的方式:

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

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

更新 2

对于乔纳森(不是罗素)克劳:

我将 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'");
    }
});

最佳答案

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

关于javascript - 为什么我的 jQuery focusout(或模糊)事件没有被触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251023/

相关文章:

javascript - 国际电话号码 jQuery 插件 : flags and phone placeholder missing

javascript - 每个 Javascript 函数都必须返回一个值吗?

javascript - 如何在 Mongo 中减去 2 个日期?

javascript - 如何打印所有选中的复选框?

javascript - 滚动浏览彼此堆叠的 div

html - 如何在多个 div 中重用 angularjs 条形图指令

jquery - 使用jquery在多个页面中的下拉菜单

html - Bootstrap 的缩略图不居中

javascript - 从模态 Bootstrap 调用操作

javascript - TinyMce4 dom添加可拖动元素(jquery)