javascript - 如何将值从复选框数组传递到 3 个输入,而不覆盖
问题描述
我想将复选框数组的值传递给 3 个输入,如果我签入一个发送到第一个输入的值,然后将第二个复选框发送到第二个输入,然后将第三个复选框发送到第三个输入,那么如果我选择另一个值没有先取消选中复选框值就不会覆盖的复选框
可以放一个addeventlistener
<form action="#" method="post" class="demoForm" id="demoForm">
<fieldset>
<label><input type="checkbox" name="sports[]" value="cycling" /> cycling</label>
<label><input type="checkbox" name="sports[]" value="running" /> running</label>
<label><input type="checkbox" name="sports[]" value="visit gym" /> visit gym</label>
<label><input type="checkbox" name="sports[]" value="swimming" /> swimming</label>
<label><input type="checkbox" name="sports[]" value="team sports" /> team sport(s)</label>
<label><input type="checkbox" name="sports[]" value="other" /> other</label>
</fieldset>
</form>
<script type="text/javascript">
var sports = document.forms['demoForm'].elements[ 'sports[]' ];
for (var i=0, len=sports.length; i<len; i++) {
sports[i].onclick = doSomething;
}
function doSomething() {
y=document.getElementById("chk1");
var sports = document.forms['demoForm'].elements[ 'sports[]' ];
if ( this.checked) {
y.value=this.value;
} else {
y.value="";
}
}
</script>
<input type="text" name="chk1" id="chk1">
<input type="text" name="chk2" id="chk2">
<input type="text" name="chk3" id="chk3">
我希望看到解决方案。
解决方案
我创建了一组条件代码,当我理解您的问题时,它会为您工作,并在此过程中通过注释进行了解释。
var sports = document.forms['demoForm'].elements[ 'sports[]' ];
for (var i=0, len=sports.length; i<len; i++) {
sports[i].onclick = doSomething;
}
function doSomething() {
x = document.getElementById("chk1");
y = document.getElementById("chk2");
z = document.getElementById("chk3");
/*if a checkbox is checked and the first input is empty, then put the checkbox
value in the first input */
if ( this.checked && x.value =="") {
x.value = this.value;
/*now if the first input is filled already, check if the second is empty and put
the checkbox value into the second*/
} else if (this.checked && y.value =="") {
y.value = this.value ;
/*now if the second input is filled already, check if the third is empty and
put the checkbox value into the third*/
} else if (this.checked && z.value ==""){
z.value = this.value ;
/* if a checkbox is checked and all 3 inputs are filled already so it gets to
this condition, then alert the user and make the checkbox shouldn't be checked*/
}else if (this.checked) {
alert("First un-check another box to check this box");
this.checked = false;
/* if the user unchecked a checkbox, then check in which input the value of this
checkbox was displayed and empty this input */
} else if (!this.checked){
switch(this.value){
case y.value:
y.value="";
break;
case x.value:
x.value="";
break;
case z.value:
z.value="";
}
}
}
推荐阅读
- r - 使用 lat 和 Lon 从栅格中提取深度数据
- java - 为什么 getAsPrimitive 和 applyAsPrimitive 而不是 get 和 apply?
- c# - 寻找合适的多线程锁
- javascript - 节点环境变量:它们会持续存在吗?
- flutter - 如何在 PageView 中使用带有 ListView 的三元组
- react-native - React Native 论文 DataTable.Cell 溢出文本导致隐藏全文
- ruby-on-rails - 自动执行 rake 任务以在 heroku 上启动时运行?
- html - 在表格的 td 中使用分词后水平滚动不起作用
- rust - 捕获外部变量的 FnOnce 闭包
- python - 通过单独将其作为对象传递,超级和继承之间有什么区别?