如果单击复选框,则复选框的值必须为1.如果未单击复选框,则其值必须为0.出于某种原因,下面的代码似乎无法正常工作。
// Checkbox values 1/0.
$("#prefForm").submit(function() {
$("input[type=checkbox]").each(function() {
var self = $(this);
if (self.is(":checked")) {
self.val('1');
} else {
self.val('0');
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select-activity">
<ul>
<li>
<!-- <input type="hidden" name="donate_buskie" value=""> -->
<input type='checkbox' id='checkbox-1' class='pseudo-checkbox sr-only' name="donate_buskie" value="0" />
<label for='checkbox-1' class='glyphicon-check-click fancy-checkbox-label'><span><?php echo JText::_('When someone donates to my Buskie'); ?></span>
</label>
</li>
<li>
<!-- <input type="hidden" name="receives_donation" value=""> -->
<input type='checkbox' id='checkbox-2' class='pseudo-checkbox sr-only' name="receives_donation" value="0" />
<label for='checkbox-2' class='glyphicon-check-click fancy-checkbox-label'><span><?php echo JText::_('When someone receives a donation'); ?></span>
</label>
</li>
<li>
<input type='checkbox' id='checkbox-3' class='pseudo-checkbox sr-only' name="comment_buskie" value="0" />
<label for='checkbox-3' class='glyphicon-check-click fancy-checkbox-label'><span><?php echo JText::_('When someone posts a comment on your Buskie'); ?></span>
</label>
</li>
<li>
<input type='checkbox' id='checkbox-4' class='pseudo-checkbox sr-only' name="comment_reply" value="0" />
<label for='checkbox-4' class='glyphicon-check-click fancy-checkbox-label'><span><?php echo JText::_('When someone replies to my comment'); ?></span>
</label>
</li>
</ul>
&#13;
答案 0 :(得分:1)
没有必要使用jQuery为复选框分配1/0值,因为它是否经过检查。该复选框已包含布尔值。如果您需要 1和0,则可以使用
var number_value = self.val() ? 1 : 0;