我試圖選擇表單中的所有按鈕,其中包括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;
});
});
請發表您的HTML和的jsfiddle。如果可能,請使用網絡示例 – j08691
下面是一個輸出所有三個按鈕的小提琴:http://jsfiddle.net/FkfSU/ –
查看我的編輯。我包含了代碼。 –