![](/img/trans.png)
[英]How to set selectedindex of select2 (version 3.5) dropdown in javascript or jQuery
[英]how to set selectedindex of select2 dropdown in javascript
我在asp.net下拉列表中为其添加了select2和一个清除按钮。 当我单击清除按钮时,我希望能够将其设置为零索引。 下面是代码:
如果删除select2部分,则可以使用“ ddlPatient.selectedIndex = 0;
”清除下拉列表ddlPatient.selectedIndex = 0;
$(document).ready(function() { debugger; $("[id$=ddlPatients]").select2(); });
function btnClear_Click() {
debugger;
var ddlPatient = document.getElementById("<%=ddlPatients.ClientID %>");
ddlPatient.selectedIndex = 0;
}
<asp:DropDownList ID="ddlPatients" runat="server"></asp:DropDownList>
<asp:Button ID="btnClear" runat="server" CssClass="btn btn-info" Text="Clear" OnClientClick="btnClear_Click();return false;" />
<asp:DropDownList ID="ddlPatients" runat="server" ClientID="”Echo”" ClientIDMode="Static"/>
然后在脚本中尝试...
var $ddlPatients = $("select[name$=ddlPatients]");
$('#ddlPatients').select2('val', '0');
要么
$('#ddlPatients')。select2()。select2('val','0');
采用
$(document).ready(function () { debugger; $("[id$=<%=ddlPatients.ClientID %>]").select2(); });
代替
$(document).ready(function () { debugger; $("[id$=ddlPatients]").select2(); });
您在一个地方使用ddlPatients.ClientID
,但硬编码另一个。
如果更改基础<select>
元素的值,则需要触发其change
事件以使Select2控件自身进行更新以匹配。
document.getElementById("<%=ddlPatients.ClientID %>");
ddlPatient.selectedIndex = 0;
$(ddlPatient).change(); // Trigger the change event.
要么:
$("<%=ddlPatients.ClientID %>").prop('selectedIndex', 0).change();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.