vb.net 如何使用javascript函数在下拉选择值上启用或禁用asp按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21671172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:44:18  来源:igfitidea点击:

How to enable or disable a asp button on dropdown selection value using javascript function?

javascriptasp.netvb.netjavascript-events

提问by sona

I have asp dropdown and button, I want to make the button always disable when dropdown selected value is index 0 and enable when some other value is selected.

我有 asp 下拉菜单和按钮,我想让按钮在下拉选择的值为索引 0 时始终禁用,并在选择其他值时启用。

my code but it is not working

我的代码,但它不起作用

function checkSelect()
{
if (document.getElementById('ddlSte').value == 'Select One')
   document.getElementById('StGoBtn').disabled = true;
else
   document.getElementById('StGoBtn').disabled = false;
}

controls:

控制:

<asp:DropDownList ID="ddlSte" runat="server" Width="162px" onchange='checkSelect(this);'>
             </asp:DropDownList>

 <asp:Button ID="StGoBtn" CssClass="buttnStyle" runat="server" Text="GO>>"></asp:Button>

采纳答案by ????

use <%=ddlSte.ClientID %>and <%=StGoBtn.ClientID %>in your code.

在您的代码中使用<%=ddlSte.ClientID %><%=StGoBtn.ClientID %>

function checkSelect()
{
   if (document.getElementById('<%=ddlSte.ClientID %>').value == 'Select One')
       document.getElementById('<%=StGoBtn.ClientID %>').disabled = true;
   else
       document.getElementById('<%=StGoBtn.ClientID %>').disabled = false;
}

The Id of control get changed If you use msterpagor user controlso you need to use ClientID to get the actual IP

控件的ID得到改变如果你使用msterpag或者user control所以你需要使用客户端ID来获得实际IP

More detail about client ID
http://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature

有关客户端 ID 的更多详细信息
http://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature

回答by adripanico

Controls in Asp.NET have not the same ID as you have declared in your code.

Asp.NET 中的控件与您在代码中声明的 ID 不同。

First approach to get your solution working is to declare your DownDropListand your Buttonwith the attribute ClientIDModeto Static. This way, rendered ID will be the same you declared in your code.

第一种方法,让您的解决方案的工作就是声明你DownDropList和你的Button同属性ClientIDModeStatic。这样,呈现的 ID 将与您在代码中声明的相同。

Best approach, is to use the @?h?kha? proposal. You can, for example, define the onchangeattribute as:

最好的方法,是使用@?h?kha? 提议。例如,您可以将onchange属性定义为:

onchange='checkSelect(<%=ddlSte.ClientID %>, <%=StGoBtn.ClientID %>);'

And then redefine your checkSelectfunction as:

然后将您的checkSelect功能重新定义为:

function checkSelect(ddlID, buttonID) { ... }

回答by Ajay

I got this result by using jquery.

我通过使用 jquery 得到了这个结果。

    <script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#<%=StGoBtn.ClientID%>").hide();
    $("#<%=ddlSte.ClientID%>").change(function () {

        if ($("#<%=ddlSte.ClientID%>").val() == "Select One") {
            $("#<%=StGoBtn.ClientID%>").hide();               
        }
        else {
            $("#<%=StGoBtn.ClientID%>").show();

        }

    });

});
</script>

Note:

笔记:

1.Inorder to work with jquery we need to add below url at head section of your html page

1.为了使用jquery,我们需要在你的html页面的head部分添加下面的url

https://code.jquery.com/jquery-1.10.2.min.js

https://code.jquery.com/jquery-1.10.2.min.js

2.The DropDown Default value(like "Select One") must be the value which you compare the value in jquery code i.e;if ($("#<%=ddlSte.ClientID%>").val() == "Select One")

2.下拉默认值(如“选择一个”)必须是你在jquery代码中比较值的值,即;if ($("#<%=ddlSte.ClientID%>").val() == "选择一个")

3.You no need to add a function in your dropdown html.i.e; simply you can write it as show below.

3.您无需在下拉列表html.ie中添加功能;简单地你可以把它写成如下所示。

<asp:DropDownList ID="ddlSte" runat="server" Width="162px"></asp:DropDownList>

回答by Nimmi

try like this

像这样尝试

In ASPX

在 ASPX

   <div>  
    <select id="ddlSte" onchange="test();">
    <option value="0">select</option>
    <option value="1">1</option>
    <option value="2">2</option>
    </select> 
   </div>

In JavaScript

在 JavaScript 中

   <script type="text/javascript"> 

    $(document).ready(function () {

        if (document.getElementById('ddlSte').value == '0')
            document.getElementById('StGoBtn').disabled = true;
        else
            document.getElementById('StGoBtn').disabled = false;
    });
    function test() {

        if (document.getElementById('ddlSte').value == '0')
            document.getElementById('StGoBtn').disabled = true;
        else
            document.getElementById('StGoBtn').disabled = false;
    }
    </script>