如何根据从数据库加载的数据填充单选按钮的已检查状态?
我想获取此表单的数据单选按钮
<div class="col-md-4">
<label>
<input type="radio" name="menu_status" class="flat-red" value="1"> Enable
</label>
<label>
<input type="radio" name="menu_status" class="flat-red" value="0"> Disable
</label>
这是我的数组结果
{"menu_id":"00001","parent_id":"0 ","menu_name":"Dashboard","menu_title":"Dashboard","url":"dashboard_c","position_menu":"1","parent_status":"0","menu_status":"1","fa_icon":"fa-dashboard","type":"1"}
我通过像这样的ajax传递结果
$.ajax({
url : "<?php echo site_url('cpanel/form_c/data_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="menu_id"]').val(data.id);
$('[name="parent_id"]').val(data.parent_id);
$('[name="menu_name"]').val(data.menu_name);
$('[name="menu_title"]').val(data.menu_title);
$('[name="url"]').val(data.url);
$('[name="order"]').val(data.position_menu);
$('[name="parent_status"]').val(data.parent_status);
//$('[name="menu_status"]').val(data.menu_status);
$('[name="fa_icon"]').val(data.fa_icon);
$('[name="type_form"]').val(data.type);
$.each (data, function (i, obj) {
$(':radio[name="menu_status"][value=' + obj.menu_status + ']').prop('checked', true);
});
/*if (data.parent_status===1) {
$('[name="menu_status"]').val("1").prop('checked',true);
} else {
$('[name="menu_status"]').val("0").prop('checked',true);
}*/
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Menu'); // Set title to Bootstrap modal title
},
有人可以帮我拿json数据来选择合适的选择吗?
答案 0 :(得分:0)
var data = {"menu_id":"00001","parent_id":"0 ","menu_name":"Dashboard","menu_title":"Dashboard","url":"dashboard_c","position_menu":"1","parent_status":"0","menu_status":"1","fa_icon":"fa-dashboard","type":"1"}
console.log(data.menu_status)
$('input[name=menu_status][value='+data.menu_status+']').prop('checked', true)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
<label>
<input type="radio" name="menu_status" class="flat-red" value="1">Enable
</label>
<label>
<input type="radio" name="menu_status" class="flat-red" value="0">Disable
</label>
</div>
尝试这种方式,检查单选按钮将等于data.menu_status
值
答案 1 :(得分:0)
看起来单选按钮类是泛红。你可以简单地使用这个而不是复杂的选择器。
您可以改用条件。
if (data.parent_status == 1) {
$('.flat-red').checked = true;
}