![](/img/trans.png)
[英]How do I limit the amount of checkbox checked with JavaScript(no jquery please)
[英]In javascript how do I refer to this.form.checkbox[5].checked?
我有一系列复选框和输入 type="text" 区域,其中当文本区域中的值更改时,我需要将复选框的 state 设置为 true。 很简单。 我已经成功地做到了:
<input name="fixed" type="checkbox">
<input name="stuff" type="text" onchange="this.form.fixed.checked=true">
哪个工作正常。 您必须编辑该字段然后单击它,但这对我的需要很好:
...但是当我切换到这个时:
<input name="fixed[0]" type="checkbox">
<input name="stuff" type="text" onchange="this.form.fixed[0].checked=true">
<input name="fixed[1]" type="checkbox">
<input name="stuff" type="text" onchange="this.form.fixed[1].checked=true">
编辑时,我的复选框没有任何变化:
恐怕我唯一的 javascript 技术来自谷歌搜索这类事情。 有人有比 Google on The Mountain 的 Oracle 更好的信息吗?
谢谢...
从点符号切换到括号符号!
this.form['fixed[0]'].checked
可能是您在 HTML 中混合了一些阴暗的做法,而当您在 javascript 中这样做时,它们正在破坏。
所以 javascript 中的 this.form.fixed[1] 确实说“数组 this.form.fixed 中的第二项。所以我认为这就是问题所在。尝试将元素命名为 fixed0 和 fixed1,看看是否有效。
编辑:您也可以使用括号表示法,如 Peter 所示,这应该可以在不编辑输入名称的情况下解决问题。
让自己的生活更轻松。
为您尝试引用的元素创建唯一 ID,并从您绑定事件的元素中引用它们:
<input name="fixed[0]" type="checkbox" id="fixed_0">
<input name="stuff" type="text" onchange="document.getElementById('fixed_0').checked=true">
<input name="fixed[1]" type="checkbox" id="fixed_1">
<input name="stuff" type="text" onchange="document.getElementById('fixed_1').checked=true">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.