jquery:需要一些函数来清空所有 <input> 值属性(在表单内),除了输入类型被隐藏
jquery: need some function that will empty all the <input> value attribute (that inside form) except inputs that type is hidden
我需要清空所有值,所有勾选框和所有选中项(重置里面的值)
除了具有 type="hidden" 属性的输入。
<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked" />
<select>
<option>a</option>
<option selected="selected">b</option>
<option>c</option>
</select>
<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
试试这个 (https://codepen.io/AlibiGhazi/pen/XWddyPR)
$(':input','#formname')
.not(':hidden')
.val('')
.prop('checked', false)
.prop('selected', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked" />
<select>
<option>a</option>
<option selected="selected">b</option>
<option>c</option>
</select>
<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
您还可以使用 form.reset()。在表单元素上调用此方法时,会将其所有子元素重置为赋予这些子元素的初始值。
我需要清空所有值,所有勾选框和所有选中项(重置里面的值) 除了具有 type="hidden" 属性的输入。
<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked" />
<select>
<option>a</option>
<option selected="selected">b</option>
<option>c</option>
</select>
<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
试试这个 (https://codepen.io/AlibiGhazi/pen/XWddyPR)
$(':input','#formname')
.not(':hidden')
.val('')
.prop('checked', false)
.prop('selected', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked" />
<select>
<option>a</option>
<option selected="selected">b</option>
<option>c</option>
</select>
<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
您还可以使用 form.reset()。在表单元素上调用此方法时,会将其所有子元素重置为赋予这些子元素的初始值。