简体   繁体   English

如果用户未选中复选框或单选按钮,如何为复选框或单选按钮设置一些默认值

[英]how do I set some default value for a check box or a radio button if user do not check the check box or select a button

I have a dynamically generated form using bootstrap form elements , in which I have options to add check boxes and radio buttons. 我有一个使用bootstrap表单元素动态生成的表单,在其中我可以选择添加复选框和单选按钮。 When I click the submit button, the data in the form is sent to the database as json. 当我单击提交按钮时,表单中的数据将作为json发送到数据库。 I am using the following javascript function for form submission 我正在使用以下javascript函数进行表单提交

$('#customForm').on("submit",function(event){
        var formData = JSON.stringify($("#customForm").serializeArray());


        var formURL = $(this).attr("action");

        $.ajax({
            url : formURL,
            type: "POST",
            data : formData,
            contentType : "application/json; charset=utf-8",

            success:function(data)
            {
            $("#myAlert").show();
            },

            failure:function(error)
            {
            } 
        });
        event.preventDefault();

    });

The problem is that , whenever the check box is left unchecked or no option is selected for the radio button, no data is send to the database corresponding to these fields. 问题在于,每当取消选中该复选框或未为单选按钮选择任何选项时,都不会将数据发送到与这些字段相对应的数据库。

Is there any way that I can use to set some default value for these form fields so that I get some value in the database for these fields even if the fields are not selected. 有什么方法可以为这些表单字段设置一些默认值,以便即使没有选择这些字段也可以在数据库中为这些字段获取一些值。

除了Marc B的注释,您还可以在数据库中为通常使用复选框进行不同设置的字段设置默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM