layui复选框动态赋值取值
<div class="layui-input-inline">
<input type="checkbox" id="feedback" lay-skin="primary" title="正确⽤药">
<input type="checkbox" id="feedback" lay-skin="primary" title="⾎糖监测">
<input type="checkbox" id="feedback" lay-skin="primary" title="规律饮⾷">
<input type="checkbox" id="feedback" lay-skin="primary" title="监测运动">
<input type="checkbox" id="feedback" lay-skin="primary" title="⾎压达标">
<input type="checkbox" id="feedback" lay-skin="primary" title="其他">
</div>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33<script>
layui.use('form', function() {
var form = layui.form, //只有执⾏了这⼀步,部分表单元素才会⾃动修饰成功 $ = layui.$;
//给CheckBox赋值
$(document).ready(function() {
$.ajax({
type: "GET",
url: "\\JsonData/json1.json",
success: function(data) {
var unitType = [];
unitType = data.CRCHD.split(",");
for(var j = 0; j < unitType.length; j++) {
var unitTypeCheckbox = $("input[id='feedback']");
for(var i = 0; i < unitTypeCheckbox.length; i++) {
if(unitTypeCheckbox[i].title == unitType[j]) {
unitTypeCheckbox[i].value = unitType[j];
unitTypeCheckbox[i].checked = true;
}
}
}
}
});
layui下载});
//监听提交
<('submit(demo1)', function(data) {
//layer.alert(JSON.stringify(GetCheckboxValues("Health"))), {
// title: '最终的提交信息'
//};
var New =data.field;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 var New =data.field;
New["CJKZK"] = GetCheckboxValues("Health");
New["CRCHD"] = GetCheckboxValues("activities"); New["CRCSH"] = GetCheckboxValues("Life");
New["CSFFK"] = GetCheckboxValues("feedback"); console.log(JSON.stringify(New));
return false;
});
//将checke拼接为"value1,value2,value3"
function GetCheckboxValues(Name) {
var result = [];
$("[id='"+ Name + "']:checkbox").each(function() { if($(this).is(":checked")) {
result.push($(this).attr("title"));
}
});
return result.join(",");
};
});
</script>
给CheckBox 赋值:
Json:
{
"CRCHD": "正确⽤药,规律饮⾷,监测运动,⾎压达标", "CBULM": "1,3,4,5"
}
拼接过后的CheckBox值:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论