更新现有数据时检索引导模式表单中的复选框字段的值时出现问题

Problems retrieving value to a checkbox field in bootstrap modal form while updating the existing data

本文关键字:复选框 字段 问题 模式 数据 检索 索引 更新 表单      更新时间:2023-09-26

我有一个用于更新字段的引导模式表单。问题是在尝试更新现有数据时,我可以获取表单中其他字段的值,但不能获取复选框字段的值。

这里是jquery:

function editSubmit(id){
            var name= $('#name-'+id).text(); #name-id is the id of the <td> field in table
            var work_day = $('#work_day-'+id).text();   #this is checkbox field. I am getting the value 'true' or 'false' in console but not in the form 
            $('#id_name_edit').val(name);     #id_name_edit is the id of the text input in form
            if(work_day == "True"){
                $('#id_work_day_edit').prop("checked", true);
            }
            $('#row-id-store').val(id);   #this stores the id of the data.
            $('#myEditModal').modal('show');
        }

尝试使用

var work_day = $('#work_day-'+id).is(':checked');

并且您正在使用基于字符串值的比较,请确保将两边转换为相同的大小写(小写/大写)。

这应该有效:

var work_day = $('#work_day-'+id).val();  

var x = $("#work_day-"+id).is(':checked');

它给你给定的复选框是否选中。.对或错