Do checkbox inputs only post data if they're checked?

后端 未结 12 752
盖世英雄少女心
盖世英雄少女心 2020-11-22 08:04

Is it standard behaviour for browsers to only send the checkbox input value data if it is checked upon form submission?

And if no value data is supplied, is the defa

12条回答
  •  北海茫月
    2020-11-22 08:45

    I resolved the problem with this code:

    HTML Form

    
    
    

    and the javascript function by change the checkbox value form:

    //change value of checkbox element
    function changeValueCheckbox(element){
       if(element.checked){
        element.value='on';
      }else{
        element.value='off';
      }
    }
    

    and the server checked if the data post is "on" or "off". I used playframework java

            final Map data = request().body().asFormUrlEncoded();
    
            if (data.get("is-business")[0].equals('on')) {
                login.setType(new MasterValue(Login.BUSINESS_TYPE));
            } else {
                login.setType(new MasterValue(Login.USER_TYPE));
            }
    

提交回复
热议问题