簡體   English   中英

為什么我的jQuery焦點(或模糊)事件沒有被觸發?

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

在的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中運行頁面,在控制台中查找錯誤信息,但沒有找到。

UPDATE

對於A.(不是“The”)Wolff:

這就是我在代碼隱藏中創建元素的方法:

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嗎? 這兩個事件之間的區別可以在這里找到,在你的情況下,你只想捕獲textarea的事件,因為它里面沒有任何項目。

這是一個“菜鳥錯誤” - 我忘了將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">

(沒有身份證)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM