use*_*714 1 javascript controls
如何使用JavaScript获取所有控件及其值和选定状态?最好在数组中包含所有控件(如select和radio)及其选择状态。
有可能吗?
谢谢!
表单的所有控件都可以在form.elements集合中找到。然后,您可以遍历集合并根据需要处理它们。
例如
function processForm(form) {
var control, controls = form.elements;
for (var i = 0, iLen = controls.length; i < iLen; i++) {
control = controls[i];
// Do something with the control
console.log(control.tagName + ':' + control.name + ' - ' + control.value);
}
}Run Code Online (Sandbox Code Playgroud)
<form id="form0">
<fieldset><legend>The form</legend>
<input name="inp0" value="foo"><br>
<select name="sel0">
<option value="opt0" selected>opt0
<option value="opt1">opt1
<option value="opt2">opt2
</select><br>
<input type="button" value="Process form" name="btn0" onclick="
processForm(this.form);
">
<input type="reset">
</fieldset>
</form>
<input name="outsideForm" form="form0" value="Over the fence">Run Code Online (Sandbox Code Playgroud)