Javascript 如何使用jQuery从三个输入中选择两个,但不提交、重置、按钮或隐藏?
我的表单中有三个下拉列表,我希望其中两个在表单提交时保留所选值。 目前我正在使用…重置整个表单Javascript 如何使用jQuery从三个输入中选择两个,但不提交、重置、按钮或隐藏?,javascript,jquery,forms,Javascript,Jquery,Forms,我的表单中有三个下拉列表,我希望其中两个在表单提交时保留所选值。 目前我正在使用…重置整个表单 function resetForm(){ $(':input','#formId').not(':button, :submit, :reset, :hidden' ).val('').removeAttr('selected'); } 我表格中的下拉列表如下- <select class="dropDown" id="user"></select> <
function resetForm(){
$(':input','#formId').not(':button, :submit, :reset, :hidden' ).val('').removeAttr('selected');
}
我表格中的下拉列表如下-
<select class="dropDown" id="user"></select>
<select class="dropDown" name="service" id="service">
<option value=''>---Please Select---</option>
<option value='S84'>Stock Control</option>
</select>
<select class="dropDown" id="process"></select>
---请选择---
库存控制
如果我正确理解了问题,您只想重置3个选定元素中的1个
因此,将它们添加到非选择器中
$(':input','#formId').not(':button, :submit, :reset, :hidden, #user, #service' )
不会重置
用户
和服务
选择元素您几乎找到了解决方案。有很多方法可以限制一组元素
我喜欢过滤器,因为它是自我描述的,您可以提供自定义函数来自定义行为
试着这样做:
function resetForm(ignored) {
// only select, textarea and input can have value inside a form. Immediately ignore hidden ones
$('#formId textarea, input:not([hidden]), #formId select').filter(function() {
// allows you to dynamically choose the ignored id. Or you can hard-code them.
return ignored.indexOf($(this).attr('id')) === -1
}).val('').removeAttr('selected');
}
resetForm(['process', 'user'])
看看这个哪两个必须保持该值?您不想保持选中的下拉列表是什么?id为“用户”和“服务”的下拉列表保持选中状态。