2014-11-01 63 views
0

我有这个输入:设定输入的默认值

<input name="giftwrap" type="checkbox"/> 

然而,当表单提交和复选框没有被选中,我得到一个null而不是假的。 我想要提交一个错误。

我感觉如果我给输入unchecked的默认值,它会返回false,而不是null,如果我打勾,然后取消勾选的输入我得到false

那么如何将输入设置为选中状态,以及如何将输入设置为未选中状态?

+0

为什么要'false'提交? – Cilan 2014-11-01 14:41:20

回答

0

$(function() { 
 
    $('input[name=giftwrap]').on('change', function() { 
 
    $(this).next().text(this.value = this.checked);//<-- note its assignment 
 
    }).trigger('change');//<-- trigger on page load 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input name="giftwrap" type="checkbox" /><span></span>

0

您可以添加checked属性:

<input name="giftwrap" type="checkbox" checked/> 

或者,也许你想是这样的:

<form> 
    <input type='hidden' value='0' name='selfdestruct'> 
    <input type='checkbox' value='1' name='selfdestruct'> 
</form> 

详情:Post the checkboxes that are unchecked

(阅读评论为好。)