HTML submit multiple values through one check box?

后端 未结 3 1070
梦如初夏
梦如初夏 2021-01-06 03:48

Hello I have a form that allows the user to check as many options as they like and then hit submit. Is there any way to have the input type \'checkbox\' submit more than one

相关标签:
3条回答
  • 2021-01-06 04:17

    You might try alternative using select2, see: https://select2.github.io/examples.html (Tagging support, use two options limit). Again, there is no enough information supplied to fully satisfy Your question.

    Another approach with select box and JSON is Can an Option in a Select tag carry multiple values? (can be rewritten for checkbox)

    0 讨论(0)
  • 2021-01-06 04:20

    I found a way to do this without JavaScript or Libraries using a hidden form-field instead:

    <input name="selectedValue" type="hidden" value="defaultValue">
    <input name="selectedValue" type="checkbox" value="secondValue">
    

    Now, if the checkbox is not selected, the hidden value is sent, if it is selected, the hidden value is overridden.

    0 讨论(0)
  • 2021-01-06 04:34

    From your comments, it sounds like you have some JavaScript that handles the data before it's submitted. If that's the case, you can add a data attribute to the checkbox. To use your example, you could call it data-valuetwo.

    <input type="checkbox" value="testuser" data-valuetwo="1">
    

    Then, your JavaScript can use getAttribute to retrieve the value in your data-valuetwo attribute and handle it appropriately. It could look something like this:

    var valuetwo = checkbox.getAttribute("data-valuetwo");
    
    0 讨论(0)
提交回复
热议问题