douduan5073 2018-12-05 06:21
浏览 218
已采纳

将复选框字段存入数据库

Currently, the checkbox field in the database is stored as ["B"],["C"]. I was wondering if there is anyway possible way to store the checkbox field as BC only.

$(document).ready(function() {
    "use strict";

    var role;
});

function savenewuser() {
    var url = serverURL() + "/newadmin.php";
    checklist = new Array();
    $('input:checkbox[name="roles[]"]:checked').each(function() {
        checklist.push($(this).val());
    });

    role = JSON.stringify(checklist);

    $.ajax({
        url: url,
        type: 'GET',
        data: {
            "role": role,
        },
        success: function(arr) {
            _getNewUserResult(arr);
        },
        error: function() {
            alert("error");
        }
    });
}

<div><label for="rights">Rights</label></div>
<input type="checkbox" name="roles[]" value="A">Bookings
<input type="checkbox" name="roles[]" value="B">Incident Booking
<input type="checkbox" name="roles[]" value="C">Edit User
  • 写回答

2条回答 默认 最新

  • donglankui1263 2018-12-05 06:33
    关注

    Since it looks like you're passing the value of role directly into the database, rather than using JSON.stringify to create role, you could just use Array.join method:

    var checklist = ['B', 'C'];
    var role = checklist.join('');
    console.log(role);
    console.log(JSON.stringify(checklist));

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?