select 标签中的 onchange 事件!
如果我更改菜单中的选项,下面的代码应该在 ParentSelect 中给出 1,但我得到的是 0。谁能告诉我错误是什么?
<input type="hidden" name="ParentSelect" value="0" />
<select class="style" onchange="document.forms[0].elements['ParentSelect']=1" >
<option value="NULL">NULL</option>
...
</select>
在这里,我的数据库中有两个表。它们包含一些字段,如服务器名称等。
有建议吗?
The following code should give me a 1 in ParentSelect if I change the option in the menu, but I am getting a 0. Can anyone tell me what is the error?
<input type="hidden" name="ParentSelect" value="0" />
<select class="style" onchange="document.forms[0].elements['ParentSelect']=1" >
<option value="NULL">NULL</option>
...
</select>
Here, I have two tables in the database. They contain some fields like ServerName, etc..
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您直接将值
1
分配给属性document.forms[0].elements['ParentSelect']
,而您可能想更改元素的值,可以这样实现:document.forms[0].elements['ParentSelect'].value = 1
我同意其他一些关于缺乏有效 HTML 结构的评论,我会添加,如果您要通过
document.forms[0]
引用该字段,您也可以为该字段提供一个 id 属性,这样您就可以简单地使用 'document.getElementById` ,这样您的代码就可以正常工作页面上的其他表格。无论如何,使用你的方法,这里是完整的代码:使用 id 代替:
You are directly assigning the value
1
to the propertydocument.forms[0].elements['ParentSelect']
, whereas you presumably mean to change the value of the element, which would be achieved like this:document.forms[0].elements['ParentSelect'].value = 1
I agree with some of the other comments concerning the lack of valid HTML strucutre, and I would add that if you are going to reference the field via
document.forms[0]
, you may as well give the field an id property so you can simply use 'document.getElementById` so your code works regardless of the other forms on the page. Anyway, using your method, here is the complete code:Using an id instead:
document.forms[0].elements['ParentSelect']
返回整个输入,如果您想设置它的值,请使用document.forms[0].elements['ParentSelect']
returns whole input, if you want to set it's value use