Detailed Explanation of Checkbox Valuation and Assignment Example in js
- 2021-08-31 07:09:31
- OfStack
1. Value of check box: (js part)
var checkboxdata = $( " input[name=payoperator]:checked " ).map(function() {
return $(this).val();
}).get().join(",");
<div class="form-group">
<label class="col-lg-2 col-md-2 col-sm-12 control-label"> Payment method </label>
<div class="col-lg-4 col-md-4">
<label class="checkbox-inline">
<input type="checkbox" name="payoperator" value="1"> Alipay online payment </label>
<label class="checkbox-inline">
<input type="checkbox" name="payoperator" value="2" checked="checked"> Alipay withholding payment </label>
<label class="checkbox-inline">
<input type="checkbox" name="payoperator" value="3" checked="checked"> Pay in person with Alipay </label>
</div>
</div>
name = "payoperator" must be the same in the check box for the check box to take effect
2. Assignment of check boxes:
After getting the value from the background, it must be initialized on the front page (the check boxes are not selected)
$("input [name=payoperator]"). attr ("checked", false);
var detail = detaildata.pay_operator; ( detaildata Is the value of all data fetched from the background, pay_operator Is a field of the database)
var split = detail.split(",");
for (var i = 0; i < split.length; i++) {
$("input[name=payoperator][value="+split[i]+"]").attr("checked","checked");
}
The page layout and the layout above take 1 sample.
Summarize