javascript中的自动填充表单完成
我有一个调用javascript函数的复选框。选中此复选框后,表单中的其他值将自动填写。这些其他值的类型是另一个复选框和一个选项选择字段。我有以下html格式:javascript中的自动填充表单完成,javascript,html,forms,Javascript,Html,Forms,我有一个调用javascript函数的复选框。选中此复选框后,表单中的其他值将自动填写。这些其他值的类型是另一个复选框和一个选项选择字段。我有以下html格式: <div id="NA" > <input type="checkbox" name="remove_cd" value="r_on" id="r_on_cd" />N/A: <select name="reason" id="reason_list"> <option value="1">
<div id="NA" >
<input type="checkbox" name="remove_cd" value="r_on" id="r_on_cd" />N/A:
<select name="reason" id="reason_list">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<font color="#cc0000">Reason Required</font><hr/></div>
不适用:
选择1
选择2
选择3
所需理由
在javascript中,函数中包含以下内容:
...
var y = document.getElementById("NA").children;
for(var i=0; i<y.length; y++){
y[i].checked=true;
y[i].options.selectedIndex=2;
}
...
。。。
var y=document.getElementById(“NA”).children;
对于(var i=0;i
应该读一下:
y[i].selectedIndex=2;
你的循环中有y++
而不是i++
。试试这个
for(var i=0; i<y.length; i++){
y[i].checked=true;
y[i].selectedIndex=2;
}
for(var i=0;i我不确定您到底想做什么,但请尝试以下操作:
<div id="NA" >
<input type="checkbox" name="remove_cd" value="r_on" id="r_on_cd" />N/A:
<select name="reason" id="reason_list">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<font color="#cc0000">Reason Required</font><hr/></div>
<script>
window.onload = function() {
var y = document.getElementById("NA").children; // it's not a good idea to use children array, because if you add a new element in the begining your script will fail
var checkbox = y[0]; // it is better here to use: document.getElementById('r_on_cd');
var select = y[1]; // it is better here to use: document.getElementById('reason_list');
checkbox.onclick = function() {
select.options.selectedIndex = 2;
};
}
</script>
不适用:
选择1
选择2
选择3
所需理由
window.onload=函数(){
var y=document.getElementById(“NA”).children;//使用children数组不是一个好主意,因为如果在开头添加新元素,脚本将失败
var checkbox=y[0];//这里最好使用:document.getElementById('r_on_cd');
var select=y[1];//这里最好使用:document.getElementById('reason_list');
checkbox.onclick=function(){
select.options.selectedIndex=2;
};
}
您可以将一些内容发布到JSBin或JSFIDLE吗?您的代码如何与标记匹配还不太清楚。
<div id="NA" >
<input type="checkbox" name="remove_cd" value="r_on" id="r_on_cd" />N/A:
<select name="reason" id="reason_list">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<font color="#cc0000">Reason Required</font><hr/></div>
<script>
window.onload = function() {
var y = document.getElementById("NA").children; // it's not a good idea to use children array, because if you add a new element in the begining your script will fail
var checkbox = y[0]; // it is better here to use: document.getElementById('r_on_cd');
var select = y[1]; // it is better here to use: document.getElementById('reason_list');
checkbox.onclick = function() {
select.options.selectedIndex = 2;
};
}
</script>