使用Javascript设置值时,保留onchange的下拉列表 [英] Preserve onchange for a dropdown list when setting the value with Javascript

查看:73
本文介绍了使用Javascript设置值时,保留onchange的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,其中包含更改值时运行的代码:

I have a dropdown list with a piece of code that is run when the value is changed:

<select name="SList" onchange="javascript:setTimeout('__doPostBack(\'SList\',\'\')', 0)" id="SList">

手动完成后,一切正常.选择一个选项后,将调用onchange代码.

Everything works fine when manually done. As an option is selected, the onchange code is called.

当我尝试使用Javscript更改所选值时,问题就开始了.我希望能够使用JS自动更改选定的选项,同时仍然像手动完成一样调用onchange代码.

The problem begins when I try to change the selected value using a piece of Javscript. I want to be able to automatically change the selected option using JS, whilst still having the onchange code called, exactly as if done manually.

我尝试这样称呼:

form.SList.value = "33";

选择了正确的选项,但是不会调用onchange代码.因此,我尝试这样做:

The right option is selected, but the onchange code does not get called. So then I try doing this:

form.SList.value = "33";
javascript:setTimeout('__doPostBack(\'SList\',\'\')', 0);

未选择正确的值,并且没有任何反应.

The right value is not selected and nothing happens.

仅供参考,代码是在ASP.NET和Javascript中完成的.

FYI, the code is done in ASP.NET and Javascript.

我可以运行什么来更改选定的选项,同时仍然调用onchange代码?

What can I run to change the selected option whilst still calling the onchange code?

推荐答案

尝试

form.SList.value = "33";
__doPostBack('SList','');

相反.

javascript:myfunction标签仅用于HTML中.

The javascript:myfunction tag is just for in-HTML use.

这篇关于使用Javascript设置值时,保留onchange的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆