简体   繁体   English

为什么我的jQuery焦点(或模糊)事件没有被触发?

[英]Why is my jQuery focusout (or blur) event not being fired?

At the bottom of the jsfiddle here , I have this HTML: 在的jsfiddle的底部在这里 ,我有这样的HTML:

<input type="text" id="BLAboxPaymentAmount" value="2">
</br>
<input type="text" id="BLAboxSection5Total" value="3">

..and this jQuery: ..和这个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");
    }
});

It works fine in the jsfiddle, but the event is not firing in my Sharepoint page - I enter a value in boxPaymentAmount ("5"), enter another in boxSection5Total ("6"), tab off of boxSection5Total, and I see no alert, as I should, based on this jQuery, which is virtually identical to what's in the jsfiddle: 它在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'");
    }
});

So the event is not even being fired - why not? 所以事件甚至没有被解雇 - 为什么不呢?

I ran the page in Chrome and F12d, looking for err msgs in the console, but found none. 我在Chrome和F12d中运行页面,在控制台中查找错误信息,但没有找到。

UPDATE UPDATE

For A. (not "The") Wolff: 对于A.(不是“The”)Wolff:

This is how I create the elements in the code-behind: 这就是我在代码隐藏中创建元素的方法:

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

I also changed the "textarea" to "text" in the HTML in the jsfiddle. 我还在jsfiddle的HTML中将“textarea”更改为“text”。

UPDATE 2 更新2

For Jonathen (not Russell) Crowe: 对于乔纳森(不是拉塞尔)克罗:

I changed my jQuery to the following: 我将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'");
    }
});

...and still see nothing in the console from this event handler. ...并且仍然在控制台中看不到此事件处理程序。

UPDATE 3 更新3

I have many other functions/handlers that fire just fine; 我还有许多其他功能/处理程序可以解决问题; so why this one doesn't is a conundrum. 那么为什么这个不是一个难题。 Maybe if I show what I've got (eliding some of the gory details), it may shed some light on what's going on (the failing function is the last/at the bottom): 也许如果我展示了我所拥有的(消除了一些血腥的细节),它可能会对正在发生的事情有所了解(失败的功能是最后/底部):

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

Did you try using blur instead of using focusout ? 你尝试使用blur而不是使用focusout吗? The difference between those two events can be found here and, in your case, you only want to capture the event for the textarea because it doesn't have any items inside it. 这两个事件之间的区别可以在这里找到,在你的情况下,你只想捕获textarea的事件,因为它里面没有任何项目。

It was a "rookie mistake" - I had forgotten to assign the ID to the element. 这是一个“菜鸟错误” - 我忘了将ID分配给元素。 Once I changed the code from this: 一旦我改变了代码:

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

...to this: ......对此:

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

...I saw the alert about entering the event. ......我看到了关于参加活动的警报。

So the UPDATE shows the faulty code, although it would take someone accustomed to creating the html elements dynamically in C# to catch it. 所以UPDATE显示了错误的代码,虽然习惯于在C#中动态创建html元素来捕获它。

For "full disclosure," here is the jQuery as it now stands (working as designed): 对于“完全公开”,这里是现在的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'");
    }
});

This "dawned on me" when I "inspected element" in the F12 tools and saw: 当我在F12工具中“检查元素”时看到这个“恍然大悟”,并看到:

<input name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ctl153" type="text" class="dplatypus-webform-field-input">

(no ID). (没有身份证)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM