Two radio buttons share one “id”?

后端 未结 8 2252
别那么骄傲
别那么骄傲 2020-12-29 06:59

I\'m borrowing/adapting this simple html/javascript form set up to put some data in the database. The original code uses text fields in the form, but I\'m using radio button

8条回答
  •  执笔经年
    2020-12-29 07:45

    Since you are using jQuery you can easily get the value of the selected radio button by using the :checked selector:

    $("input[name=interview]:checked").val()
    

    You should definitely not give more than one element the same ID (that's invalid HTML and will lead to confusing bugs in your JavaScript), but even if that worked it wouldn't help in this case since radio buttons as a group don't have a selected value: you need to determine which one is currently checked and then get its value as shown above. (This is not a problem when getting the value on the webserver when the form is actually submitted, because only the value from the checked radio gets submitted.)

    Note also that in your original code where you said inputUser.attr("value"), you could've said inputUser.val().

提交回复
热议问题