jquery check radio button

孤街醉人 提交于 2019-12-25 14:13:05

问题


PHP keeps saying unidentified index: gender? I'm not sure where the error is.

HTML:

<label>Gender:</label> <input type="radio" name="gender" value="1">Male <input type="radio" name="gender" value="0">Female

JQuery:

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

PHP:

$gender = $_POST['gender'];
if($gender == '') {
  echo 'Please select gender';
} 

回答1:


$("input[@name=gender]:checked")

That is an invalid selector in modern jQuery. This means that no element is found, and therefore no value is set for gender. You need to remove the @ and add quote marks:

$("input[name='gender']:checked")

See the API for the attribute-equals selector.




回答2:


sorted. thanks

gender: $('input:radio[name=gender]:checked').val()


来源:https://stackoverflow.com/questions/4684093/jquery-check-radio-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!