使用 JavaScript 重置下拉列表
我有以下场景 - 一些 dropdwon 列表和一个文本框。
当用户单击文本框时,我想将这些下拉菜单“重置”为其原始值:
Javascript:
function ResetDropDowns()
{
var ddlSuppliers = document.getElementById('<%=ddlSuppliers.ClientID%>');
var ddlResponse = document.getElementById('<%=ddlResponse.ClientID%>');
var ddlImportStatus = document.getElementById('<%=ddlImportStatus.ClientID%>');
ddlSuppliers.selectedIndex = -1;
ddlResponse.selectedIndex = -1;
ddlImportStatus.selectedIndex = -1;
}
隐藏代码:
tbxAutoCompleteSupplier.Attributes.Add("onFocus", "return ResetDropDowns();");
protected void ddlSuppliers_DataBound(object sender, EventArgs e)
{
ddlSuppliers.Items.Insert(0,
new ListItem("--Please Select Supplier--", "0"));
}
但是,这似乎不起作用。
有什么想法吗?
I have the following scenario - a few dropdwon lists and a textbox.
I'd like to 'reset' these drop downs to their original value when a user clicks on the textbox:
Javascript:
function ResetDropDowns()
{
var ddlSuppliers = document.getElementById('<%=ddlSuppliers.ClientID%>');
var ddlResponse = document.getElementById('<%=ddlResponse.ClientID%>');
var ddlImportStatus = document.getElementById('<%=ddlImportStatus.ClientID%>');
ddlSuppliers.selectedIndex = -1;
ddlResponse.selectedIndex = -1;
ddlImportStatus.selectedIndex = -1;
}
Code behind:
tbxAutoCompleteSupplier.Attributes.Add("onFocus", "return ResetDropDowns();");
protected void ddlSuppliers_DataBound(object sender, EventArgs e)
{
ddlSuppliers.Items.Insert(0,
new ListItem("--Please Select Supplier--", "0"));
}
However, this does not seem to do the business.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要将索引更改为
-1
,而默认项目位于索引0
处?应该做到这一点。
更新
您能否验证下拉菜单是否已设置?要调试 ResetDropDowns() 方法,只需在开头键入关键字
debugger
即可。这将破坏编译器,因此您可以单步执行代码。例子:
Why are you changing the index to
-1
while your default item is at index0
?Should do the trick.
Update
Can you verify if the dropdowns are set? To debug the ResetDropDowns() method just type the keyword
debugger
at the beginning. This will break the compiler so you can step through the code.Example:
这实际上是因为这个 javascript 和我们的 CMS 的 jquery 之间存在冲突 - 现在全部排序了!
解决方案:
我将控件包装在 div 中(称为包装器),然后将 .uniform 应用于该 div 中的控件。
这确保了 CSS 得到维护。
不过还是谢谢你的建议。
this is actually because of conflict between this javascript and our CMS' jquery - all sorted now!
Resolution:
I wrapped the controls in a div (called wrapper) and then applied the .uniform to the controls within said div.
That ensures the css is maintained.
Thanks for the suggestions though.