Javascript 使用 href 从选择下拉列表中选择选项
我正在开发一个 CMS 网站,该网站根据 id 和值动态创建表单元素。 我需要使用链接从这些表单中选择某些信息。
对于单选选项和复选框选项,我使用以下代码: 1001
效果很好。 RAL1 是我要检查的无线电的 ID。 使用选择时,id 后面需要跟着选项,这就是我遇到问题的地方,因为它需要从 ID 中选择一个值。
表单创建的代码是
<select id="Zinc_plated_field" class="inputboxattrib" name="Zinc_plated12">
<option value="no">no</option>
<option value="yes">yes (+€871.08)</option>
</select>
我已经尝试过的所有方法,但没有成功。有人能指出我正确的方向吗?
谢谢!
Im working on a CMS website that creates form elements on the fly according to id and values.
I need to select certain information out of these forms using a link.
For radio options and checkbox options I use this code:<a href="#" onclick="document.getElementById('RAL1').checked=true">1001</a>
Which works fine.
RAL1 is the id of radio I want to check.
With the select the id needs to be follwed by the options and this is where Im having problems because it needs to select a value out of an ID.
The code the form creates is
<select id="Zinc_plated_field" class="inputboxattrib" name="Zinc_plated12">
<option value="no">no</option>
<option value="yes">yes (+€871.08)</option>
</select>
I've tried just about everything but no luck. Can anybody point me in the right direction?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 Firefox 和 Firefox 中对我有用。野生动物园:
This works for me in Firefox & Safari:
几乎每个浏览器都支持以下语句来获取所选值(
“no”
或“yes”
):对于非常旧的浏览器,您会必须使用:
如果你想设置选定的值,有几种方法可以做到:
或
或
后两者使用你要设置的选项的索引(一个指定选择哪个索引通过设置
我无法立即说出哪个跨浏览器兼容程度最高,但如果第一个不起作用,请尝试其他两个。
Just about every browser supports the following statement to get the selected value (
"no"
or"yes"
):For very old browsers you would have to use:
If you want to set the selected value, there are several ways to do it:
or
or
The two latter use the index of the option you want to set (one specifies which index is selected by setting the
selectedIndex
property of the<select>
element, while the other sets theselected
attribute of the<option>
element totrue
.)I can't say which is most cross-browser compatible off-hand, but if the first one doesn't work, just try the other two.