我有这个jQuery:
$(document).ready(function () {
$('[id$=_AddressRows]').slideUp();
$('[id$=panelSection2]').slideUp();
$('[id$=panelSection3]').slideUp();
$('[id$=ddlPayToIndividual]').slideUp();
$('[id$=ddlPayToVendor]').slideUp();
});
......在大多数情况下,他们完成自己的工作,并隐藏他们应该遵循的元素。
但是,最近添加的两个,最后两个(对于下拉列表)不起作用。
我在C#中创建元素,如下所示:
// add the comboboxes
DropDownList ddlPaymentToIndividual = new DropDownList();
ddlPaymentToIndividual.ID = "ddlPayToIndividual";
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Select type", "0"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Damage Payment", "1"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Fellowship / Scholarship", "2"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Funded Activity", "3"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Gift (Non-cash)", "4"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Honorarium", "5"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Online Training & Development", "6"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Participant Support", "7"));
ddlPaymentToIndividual.Items.Add(new System.Web.UI.WebControls.ListItem("Prize / Award (Non-cash)", "8"));
cellRadioButton1_2.Controls.Add(ddlPaymentToIndividual);
DropDownList ddlPaymentToVendor = new DropDownList();
ddlPaymentToVendor.ID = "ddlPayToVendor";
. . .
......但他们尽管已经滑倒了但仍然显示出来。为什么呢?
相关呈现的HTML是:
<td><select name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ddlPayToIndividual" id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_ddlPayToIndividual">
<option value="0">Select type</option>
<option value="1">Damage Payment</option>
<option value="2">Fellowship / Scholarship</option>
<option value="3">Funded Activity</option>
<option value="4">Gift (Non-cash)</option>
<option value="5">Honorarium</option>
<option value="6">Online Training & Development</option>
<option value="7">Participant Support</option>
<option value="8">Prize / Award (Non-cash)</option>
</select></td>
<td><select name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$ddlPayToVendor" id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_ddlPayToVendor">
<option value="0">Select type</option>
<option value="1">Donation / Contribution</option>
<option value="2">Membership</option>
<option value="3">Postage/Outbound Freight</option>
<option value="4">Refund to Research Agency</option>
<option value="5">Utilities</option>
<option value="6">Subscription</option>
<option value="7">Other/Miscellaneous</option>
</select></td>
此外,上面显示的jQuery代码是逐字呈现的。
注意:&#34;隐藏&#34;也行不通;我尝试更改&#34; ready&#34;中的代码。功能如下:
$('[id$=ddlPayToIndividual]').hide();
$('[id$=ddlPayToVendor]').hide();
......但它没有差异。
事实证明这个代码:
$(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();
}
});
... 正在工作(选择一个单选按钮会向上滑动,另一个向下滑动)。所以它本身并不是slideUp()的问题。这是一个&#34;时间&#34;问题 - 当在ready函数中调用slideUp()时,还没有创建下拉列表,因此忽略了调用?如果是这种情况,为什么其他 slideUp()有效?
答案 0 :(得分:0)
正如“他们”所说,“时机就是一切。”这种代码必须去,而不是在“ready”函数中,而是在窗口加载函数中,如下所示:
/* 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$=ddlPayToIndividual]').hide();
$('[id$=ddlPayToVendor]').hide();
});