How do I turn a button group in Bootstrap into a radio-style input to my form?

后端 未结 5 2022
一向
一向 2021-02-04 11:31

I have the following HTML that creates a radio button group using Bootstrap.

5条回答
  •  生来不讨喜
    2021-02-04 11:53

    $('button[name="rating"].active').val();
    

    In plain English that selection reads as:

    • give me the value of
    • button elements
    • filter by the name attribute that has a value of rating (the group)
    • and the CSS class active (indicating it was selected)

    Edit based on OP's new question in comments:

    To capture the input from the button you will need to use custom script handlers. If your goal is to actually submit this with the form, I would actually suggest against it. Mostly because this is not what a user is expecting in a form. Just use a regular radio button input.

    However if you do want to use this, you can do the following:

    $('form').submit(function() {
        var rating = $('button[name="rating"].active').val();  
    
        // get the rest of the values
        // submit the form
    });​
    

    Here's an example with a regular input vs. the button and why you shouldn't use the button in a form: http://jsfiddle.net/TxgVe/1/

提交回复
热议问题