jQuery / Javascript - 单击复选框时提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18135420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery / Javascript - submit form when checkbox is clicked
提问by user2510039
I have a page where I display results form database and there are checkboxs which filter ther results. The problem is that I don't want to use submit button because I would like to let the user only click on the checkboxes to get the desired results. So is there anyway using javascript or jquery to submit a form when checkbox is checked with no submit button? if you see on shopping sites there are no submit buttons just the checkboxes..Like that. thanks
我有一个页面,我在其中显示结果表单数据库,并且有筛选结果的复选框。问题是我不想使用提交按钮,因为我想让用户只点击复选框来获得所需的结果。那么在没有提交按钮的情况下选中复选框时,是否使用javascript或jquery提交表单?如果您在购物网站上看到没有提交按钮,只有复选框......就像那样。谢谢
<form>
<input type="checkbox" name="size" > Size
</form>
here is the php
这是php
<?php if (isset($_POST["size"])){
//do something
?>
回答by itsme
<form id="form" method="post" action="">
<input type="checkbox" name="checkbox" class="checkbox"/>
</form>
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
OR
或者
<form id="form" method="post" action="">
<input type="checkbox" onchange="$('#form').submit();" name="checkbox" class="checkbox"/>
</form>
回答by Alex Hill
$(input:name=[checkboxname]).change(function(){
$('#form').submit();
});
alternately
交替
$('#checkboxId').click(function(){
$('#formId').submit();
});
of course additional parameters can be passed withing the submit() function for ajax, etc.
当然,附加参数可以通过 ajax 等的 submit() 函数传递。