godsent Posted February 12, 2010 Share Posted February 12, 2010 <p><input type="radio" name="type_select" value="1" class="styled" /> 111</p> <p><input type="radio" name="type_select" value="2" class="styled" /> 222</p> <p><input type="radio" name="type_select" value="3" class="styled" /> 333</p> I would like to check if user have selected any value like this: alert($("input[@name='type_select']:checked").val()); // if value not selected it alerts "3", and no matter if page is refreshed or just entered if ($("input[@name='type_select']:checked").val() == "unidentified") { alert("Please select any value."); } Quote Link to comment https://forums.phpfreaks.com/topic/191875-radiobutton-if-value-is-not-selected-it-shows-that-selected-the-last-jquery/ Share on other sites More sharing options...
Psycho Posted February 12, 2010 Share Posted February 12, 2010 I don't use JQuery, but I suspect it has the same quirks when dealing with radio groups as when using straight JavaScript. Radio groups are just that - groups. As such the group is referenced as a single opbject with multiple child objects - much like an array. You cannot reference the group to see if any single option is selected. You have to check each option. The easiest solution is to just check one of the optins by default as it is impossible (from the UI perspective) to unselect all th eoptions. You can only select a different option. If you stil lwant to have them all unchecked to begin with, then you will need to iterrate through each of the options to see if one is selected. Here is a javascript function I use to return the selected value of a radio group. If none are selected, the function returns false. You might be able to rewrite that for JQuery. function radioGroupValue(groupObj) { //Check if ther is only on option (i.e. not an array) if (!groupObj.length) { //Only one option in group return (groupObj.checked) ? groupObj.value : false; } //Multiple options, iterate through each option for (var i=0; i<groupObj.length; i++) { //Check if option is checked if (groupObj[i].checked) { //Return value of the checked radio button return groupObj[i].value; } } //No option was selected return false; } Quote Link to comment https://forums.phpfreaks.com/topic/191875-radiobutton-if-value-is-not-selected-it-shows-that-selected-the-last-jquery/#findComment-1011407 Share on other sites More sharing options...
bibby Posted February 13, 2010 Share Posted February 13, 2010 You're going to want to add the :checked selector to the one you have. $('[name=type_select]:checked').val() The above will return either 1, 2, 3, or nothing if there is no selection. Quote Link to comment https://forums.phpfreaks.com/topic/191875-radiobutton-if-value-is-not-selected-it-shows-that-selected-the-last-jquery/#findComment-1011719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.