使用 JavaScript 重置下拉列表

发布于 2024-09-24 10:50:37 字数 841 浏览 1 评论 0原文

我有以下场景 - 一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-10-01 10:50:37

为什么要将索引更改为 -1,而默认项目位于索引 0 处?

ddlSuppliers.selectedIndex = 0;
ddlResponse.selectedIndex = 0;
ddlImportStatus.selectedIndex = 0; 

应该做到这一点。

更新

您能否验证下拉菜单是否已设置?要调试 ResetDropDowns() 方法,只需在开头键入关键字 debugger 即可。这将破坏编译器,因此您可以单步执行代码。

例子:

function ResetDropDowns() 
{
    debugger; //compiler will break here
    var ddlSuppliers = document.getElementById('<%=ddlSuppliers.ClientID%>');
}

Why are you changing the index to -1 while your default item is at index 0?

ddlSuppliers.selectedIndex = 0;
ddlResponse.selectedIndex = 0;
ddlImportStatus.selectedIndex = 0; 

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:

function ResetDropDowns() 
{
    debugger; //compiler will break here
    var ddlSuppliers = document.getElementById('<%=ddlSuppliers.ClientID%>');
}
伴随着你 2024-10-01 10:50:37

这实际上是因为这个 javascript 和我们的 CMS 的 jquery 之间存在冲突 - 现在全部排序了!

解决方案:

我将控件包装在 div 中(称为包装器),然后将 .uniform 应用于该 div 中的控件。

function pageLoad(sender, args) {
        if (args.get_isPartialLoad()) {
            $("#wrapper select, #wrapper span, #wrapper input").uniform();
        }
    }

这确保了 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.

function pageLoad(sender, args) {
        if (args.get_isPartialLoad()) {
            $("#wrapper select, #wrapper span, #wrapper input").uniform();
        }
    }

That ensures the css is maintained.

Thanks for the suggestions though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文