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

将复选框字段存入数据库

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条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝