【问题标题】:Why does this jQuery cause *all* my jQuery to fail?为什么这个 jQuery 会导致 *all* my jQuery 失败?
【发布时间】:2015-09-06 00:29:00
【问题描述】:

我添加了这个 jQuery 以尝试有条件地显示某些元素:

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

此代码基于来自here 的代码;我将“is”更改为“not”,因为我需要在行可见时采取行动。

...但它不仅不起作用(单击“+”按钮 (btnAddFoapalRow) 不会使行可见),它还会导致它之前的另一个 jQuery 也不起作用。上面的 jQuery 有什么问题,为什么这么突兀/碍事?

这里是所有 jQuery 的上下文/您的阅读乐趣:

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

这是在 Sharepoint 2010 项目中。我也尝试使用服务器端(C#)代码来完成同样的事情,但这也不起作用,因为每次单击按钮都会再次提交页面。我在这里问过这个问题 [Why does my no-submit (HtmlButton) still submit?

相关的代码隐藏是:

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;

更新

我把 jQuery 改成了这个,所以我可以验证代码是否被访问(添加了对 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();
    }
});

...我确实在 Chrome 的控制台中看到“到达 foapalrow3 不可见分支”,但页面上没有任何视觉反应。

我想知道我是否真的需要这样的东西:

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

而不是这个:

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

?如果是这样,那么正确的语法是什么(将可见设置为 true)?

更新 2

我试过了:

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

...但在 Mudville 没有乐趣。

【问题讨论】:

  • 错误控制台说什么?
  • 缺少括号。示例:if $('[id$=foapalrow3]').not(":visible"); 应该是 if ($('[id$=foapalrow3]').not(":visible"))。与else if 相同。
  • 不应该 not(":visible");not(":visible") { 吗?花括号在您的代码中不匹配。

标签: c# jquery sharepoint-2010 html-table htmlbutton


【解决方案1】:

这是完全错误的:

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

改成:

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

【讨论】:

  • 它不再处理 jQuery 的其余部分,但它仍然不起作用。也许从 C# 的角度来看可见并不等同于 jQuery 对可见的理解/定义?顺便说一句,我因为“完全错误”而感到兴奋,因为我不得不盯着这两个街区,直到我开始觉得自己像独眼巨人,然后才注意到不同之处。
  • 能说一下控制台的内容吗?
  • 复制网址并尝试用Chrome打开!
  • 嘿,资源后面是什么,应该是Audits,然后是Console。全屏签入或最大化开发工具!
  • 是的,另一个 jQuery 现在可以正常工作了——只是这个什么都不做(没什么不好,没什么好——没什么);这是选美前的 Boo Radley。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2022-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多