我正在尝试选择表单中的所有按钮,其中包括reset
和submit
元素。我当前的选择器是这样的:
:button, [type=\"submit\"], [type=\"reset\"]
但是,它不适用于reset
.
这是我的代码:
HTML:
<form>
<input type="text" />
<input type="text" />
<input type="text" />
<select>
<option>hello</option>
<option>hi
</option>
<option></option>
</select>
<textarea></textarea>
<input type="checkbox" name="hi" value="hi" />
<input type="radio" name="test" value="hi" />
<input type="radio" name="test" value="hello" />
<input type="radio" name="test" />
<input type="button" />
<button></button>
<input type="reset" />
<input type="submit" />
</form>
JS:
$(document).ready(function() {
$("form").submit(function() {
var shouldSubmit = true;
$(this).children(":input:not(:button, [type=\"submit\"], [type=\"reset\"])").each(function() {
if ($(this).val() == "")
{
shouldSubmit = false;
return shouldSubmit;
}
});
if ($("input:checkbox").length > 0)
{
if ($("input:checkbox:checked").length == 0)
shouldSubmit = false;
}
if ($("input:radio").length > 0)
{
if ($("input:radio:checked").length == 0)
shouldSubmit = false;
}
if (shouldSubmit == false)
alert("All form fields must be filled out.");
return shouldSubmit;
});
});