我有四個複選框。我需要檢查是否選擇了至少2個。他們不在任何DIV內部。他們是獨立的。僅當選擇至少2個複選框時纔會提交表格。jQuery複選框計數選擇
-4
A
回答
0
請嘗試此代碼。
<form id="submitForm" method="POST">
<input type="checkbox" name="checkboxname" id="checkboxname" value="check1"> check1
<input type="checkbox" name="checkboxname" id="checkboxname" value="check2"> check2
<input type="checkbox" name="checkboxname" id="checkboxname" value="check3"> check3
<input type="checkbox" name="checkboxname" id="checkboxname" value="check4"> check4
<input type="submit" name="proceed" value="proceed" id="proceed">
</form>
和jQuery腳本是
$(document).ready(function(){
$('#proceed').click(function(){
alert('im triggered')
alert($('#checkboxname:checked').length)
});
});
試試吧。
+0
亞洲感謝噸似乎工作:) –
0
完整代碼:
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Playground</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<h1>Submit Form</h1>
<p>Select two options below and submit form</p>
<form action="action.php" method="post" id="myForm" enctype="application/x-www-form-urlencoded">
<p>Select only 2 fruits you would like to have for dinner</p>
<input type="checkbox" name="fruit" value="orange" class="fruit"> Oranges<br>
<input type="checkbox" name="fruit" value="banana" class="fruit"> Bananas<br>
<input type="checkbox" name="fruit" value="apple" class="fruit"> Apples<br>
<input type="checkbox" name="fruit" value="grape" class="fruit"> Grape<br>
<input id="go" type="submit" value="Go Button">
</form>
<script>
jQuery(function($){
$("#myForm").on("submit", function(e){
// check if 2 items have been checked
if($(".fruit:checked").length !== 2){
e.preventDefault();
return false;
}
// continue form submission if 2 items have been checked
});
});
</script>
</body>
相關問題
- 1. 計數選擇複選框
- 2. jQuery 1.9複選框計數
- 3. jQuery複選框樹選擇
- 4. jquery選擇器複選框
- 5. jQuery複選框選擇
- 6. Jquery複選框選擇
- 7. jQuery選擇複選框
- 8. 選擇複選框/ jQuery的
- 9. jquery複選框選擇總計在一個選擇
- 10. 計數與jquery選定的複選框
- 11. Jquery複選框多選和行選擇
- 12. 選擇複選框選擇
- 13. 複選框計數
- 14. jQuery增量計數選定的複選框和取消選中的複選框
- 15. 計算從複選框+選擇列表
- 16. Jquery複選框選擇/取消選擇最佳給定X複選框
- 17. jQuery:計數選中的複選框 - 錯誤的計數
- 18. 選擇複選框
- 19. 選擇複選框
- 20. 選擇複選框
- 21. 用JQuery選擇複選框ID
- 22. JQuery隱藏()和複選框選擇
- 23. jquery複選框選擇操縱行表
- 24. jQuery的 - JsTree複選框選擇不同
- 25. 複選框選擇與jQuery的問題
- 26. 發現選擇複選框使用jQuery
- 27. jquery複選框選擇值條件
- 28. jQuery選擇所有複選框與toggleClass
- 29. jQuery的,動態的複選框選擇
- 30. 在facebox和jquery中選擇複選框?
請張貼的HTML片段,以及顯示你已經嘗試過jQuery的片段。 – Brian
'$('。checkboxes:checked')。length> 1' – adeneo
還有其他部分也有複選框,所以不能直接使用它。 –