Javascript 如何使用asp.net下拉列表调用javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14811718/
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-08-24 18:00:25  来源:igfitidea点击:

How to call a javascript function using asp.net dropdown list

javascriptasp.net

提问by Optimus

I need to call a javascriptfunction when the dropdownlist is changed.

javascriptdropdown列表更改时,我需要调用一个函数。

How can I implement that in asp.net?

我该如何实现asp.net

回答by Stardust

Use onchangeevent to excecute function whenever the dropdown list clicked.

onchange每当单击下拉列表时,使用事件执行函数。

<select id="mylist" onchange = "go()">
<option value="value1">value1</option>
<option value="value2">value2</option>
</select>

<script>
  function go()
  {
    var x = document.getElementById("mylist").value;
    console.log(x);
  }
</script>

回答by Viktor S.

<asp:DropDownList runat="server" ID="DDList" onclick="alert(1)"></asp:DropDownList>

If you want to get function executed when element clicked, you can use above code to define a function that should be executed 'onclick'.

如果你想在元素被点击时执行函数,你可以使用上面的代码来定义一个应该在“onclick”上执行的函数。

But it is better to use something like addEventListener, just search for cross-browser function (for instance, like addListener function here):

但最好使用类似的东西addEventListener,只需搜索跨浏览器函数(例如,像这里的addListener 函数):

document.getElementById("<%=DDList.ClientID %>").addEventListener("click", fucntionToExecuteName, false)

Remember, than in this case you must take DDList.ClientIDand use it as id of an element, because it will be different from ID you have set in your aspx code

请记住,在这种情况下,您必须将DDList.ClientID其用作元素的 id,因为它将与您在 aspx 代码中设置的 ID 不同

But if you need some function to be executed when actual value is changed, you should use onchangeevent.

但是,如果您需要在更改实际值时执行某些函数,则应该使用onchangeevent。

回答by Neel

Use something like this (uses jQuery):

使用这样的东西(使用 jQuery):

$(document).ready(function() {
    $("#dropdownId").change(function(e)) {
             do something...
    });
});

回答by Chukwuemeka

Add this script to your mark-up and be sure to also include a script reference to jquery:

将此脚本添加到您的标记中,并确保还包含对 jquery 的脚本引用:

$(document).ready(function()
{
     $("#yourDropdownId").change(function(){
        //Todo: write your javascript code here.
 });
});

Make sure that the control with "yourDropdownId" as ID has the property: "ClientIDMode" set to static or the "all-knowing" ASP.NET engine will auto-generate an element name for the resulting html with parent element names appended to the control by default.

确保以“yourDropdownId”为 ID 的控件具有以下属性:“ClientIDMode”设置为静态或“全知”ASP.NET 引擎将为结果 html 自动生成元素名称,并将父元素名称附加到默认控制。

回答by ssilas777

Use Jquery:

使用jQuery

$(document).ready(function(){
    $("#DropDownID").change(function () {
           // Your requirment
     });
});

Also it's always better to write it inside the document.ready

另外最好把它写在里面 document.ready

回答by Harshit Tailor

Use JQuery

使用 JQuery

$(document).ready(function(){
    $('select[name$=DrpGoingTo]').change(function () {
       //Code here
    });
});

回答by user6336440

You can use an onchangeevent and call it from itself:

您可以使用onchange事件并从其自身调用它:

<asp:DropDownList ID="DropdownList" runat="server" onchange="javascript:MyFunction();" >
</asp:DropDownList>