Why does this jQuery cause *all* my jQuery to fail?

浪子不回头ぞ 提交于 2019-12-13 04:31:25

问题


I've added this jQuery in an attempt to conditionally show certain elements:

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    if $('[id$=foapalrow3]').not(":visible");
        $('[id$=foapalrow3]').slideDown();
    } 
    else if $('[id$=foapalrow4]').not(":visible");
        $('[id$=foapalrow4]').slideDown();
    }
});

This code is based on code from here; I changed the "is" to "not" because I need to act when the row is not visible.

...but not only does it not work (clicking the "+" button (btnAddFoapalRow) does not make the rows visible), it causes the other jQuery that precedes it to be non-functional, too. What is wrong with the jQuery above, and why is it so obtrusive/obstructionistic?

Here is all the jQuery for context/your reading pleasure:

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

/* If the 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;
    if (ckd) alert('Please log in prior to continuing with this form');
});

/* If the select "Yes" (they are seeking payment for themselves, as opposed to someone else), omit (invisibilize) sections 2 and 3 on the form */
$(document).on("change", '[id$=ckbxPaymentForSelf]', function () {
    if (this.checked) {
        $('[id$=panelSection2]').slideUp();
        $('[id$=panelSection3]').slideUp();
        $('[id$=rbPaymentToIndividual]').prop("checked", true);
        $('[id$=_MailStopRow]').slideDown();
        $('[id$=_AddressRows]').slideUp();
    }
    else {
        $('[id$=panelSection2]').slideDown();
        $('[id$=panelSection3]').slideDown();
        $('[id$=rbPaymentToIndividual]').prop("checked", false);
        $('[id$=_MailStopRow]').slideUp();
        $('[id$=_AddressRows]').slideDown();
    }
});

/* When "UCSC insider" checkbox changes state, set up txtbxSSNOrITIN accordingly - was ckbxEmp, which has been deprecated/removed */
$(document).on("change", '[id$=ckbxUCSCFacultyStaffOrStudent]', function () {
    var ssnmaxlen = 4;
    var itinmaxlen = 11;
    var ssntextboxwidth = 40;
    var itintextboxwidth = 100;
    var ckd = this.checked;
    var $input = $('[id$=txtbxSSNOrITIN]');
    var $lbl = $('[id$=lblSSNOrITIN]');

    if (ckd) $input.data("oldValue", $input.val()); // Remember current value

    $input.prop("maxlength", ckd ? ssnmaxlen : itinmaxlen).css({
        background: ckd ? 'yellow' : 'lightgreen',
        width: ckd ? ssntextboxwidth : itintextboxwidth
    }).val(function (i, v) {
        /* If checked, trim textbox contents to ssnmaxlen characters */
        return ckd && v.length > ssnmaxlen ? v.slice(0, ssnmaxlen) : $input.data("oldValue");
    });

    $lbl.text(ckd ? "SSN - last 4 digits" : "ITIN");
    /* This sets focus to the end of the textbox (multiplication by 2 is because some characters are counted as two) */
    var strLength = $input.val().length * 2;
    $input.focus();
    $input[0].setSelectionRange(strLength, strLength);
});

$(document).on("keypress", '[id$=txtbxSSNOrITIN]', function (e) { 
    /* For now, just "eating" non-numeric entries (from http://jsfiddle.net/zpg8k/); will change when the business rules for ITIN are known */
    var k = e.which;
    if (k < 48 || k > 57) { e.preventDefault(); }
});

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    if $('[id$=foapalrow3]').not(":visible");
        $('[id$=foapalrow3]').slideDown();
    } 
    else if $('[id$=foapalrow4]').not(":visible");
        $('[id$=foapalrow4]').slideDown();
    }
});

This is in a Sharepoint 2010 project. I've tried to accomplish the same thing using server-side (C#) code, too, but that also is not working, due to each click of the button submitting the page again. I asked about that here [Why does my no-submit (HtmlButton) still submit?

The pertinent code-behind is:

HtmlButton btnAddFoapalRow = null;
. . .       
btnAddFoapalRow = new HtmlButton();
btnAddFoapalRow.Attributes["type"] = "button";
btnAddFoapalRow.InnerHtml = "+"; 
btnAddFoapalRow.ID = "btnAddFoapalRow";
this.Controls.Add(btnAddFoapalRow);    


foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";
foapalrow3.Visible = false;
. . .
foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";
foapalrow3.Visible = false;

UPDATE

I changed the jQuery to this, so I could verify the code is being reached (added the calls to console.log()):

$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
    if ($('[id$=foapalrow3]').not(":visible")) {
        console.log('reached the foapalrow3 not visible branch');
        $('[id$=foapalrow3]').slideDown();
    }
    else if ($('[id$=foapalrow4]').not(":visible")) {
        console.log('reached the foapalrow4 not visible branch');
        $('[id$=foapalrow4]').slideDown();
    }
});

...and I do see 'reached the foapalrow3 not visible branch' in the console in Chrome, but nothing happens visually on the page.

I wonder if I really need something like this:

$('[id$=foapalrow3]').visible();

instead of this:

$('[id$=foapalrow3]').slideDown();

? If so, what is the proper syntax for that (setting visible to true)?

UPDATE 2

I tried this:

$('[id$=foapalrow3]').attr("visibility", "visible");

...but no joy in Mudville.


回答1:


This is totally wrong:

if $('[id$=foapalrow3]').not(":visible");
    $('[id$=foapalrow3]').slideDown();
} 
else if $('[id$=foapalrow4]').not(":visible");
    $('[id$=foapalrow4]').slideDown();
}

Change it to:

if ($('[id$=foapalrow3]').not(":visible")) {
    $('[id$=foapalrow3]').slideDown();
} 
else if ($('[id$=foapalrow4]').not(":visible")) {
    $('[id$=foapalrow4]').slideDown();
}


来源:https://stackoverflow.com/questions/30948026/why-does-this-jquery-cause-all-my-jquery-to-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!