Thursday, 15 March 2012

javascript - Disable submit button until one in a group of dynamically-created radio buttons selected -



javascript - Disable submit button until one in a group of dynamically-created radio buttons selected -

i disable submit button until 1 of grouping of radio buttons selected. know there similar questions out there, none pertain dynamically-created grouping of radio buttons...

here have.. script @ top of page generates number of buttons given user upload in previous view:

var jscriptarray = new array(@viewbag.colnames.length); var array = @html.raw(json.encode(viewbag.colnames)); for( var = 0; < @viewbag.colnames.length; i++ ) { jscriptarray[i] = array[i]; } var length = @(viewbag.ncols); $(document).ready(function () { (var = 0; < length; i++) { $('#radiogroupby').append('<input id="grp' + +'" type="radio" name="group" value="'+i+'">'+jscriptarray[i]+'</input>') $('#radiogroupby').append('<p style="padding:0px;margin:0px;"></br></p>'); } });

this works, , selecting of buttons returns proper value; great. however, want disable submit button until 1 of these radio buttons selected. using reply found on earlier, created next (this works, if hard code grouping of buttons. issue won't work javascript-created group):

var $radiobuttons = $("input[name='group']"); $radiobuttons.change(function () { var anyradiobuttonhasvalue = false; // iterate through radio buttons $radiobuttons.each(function () { if (this.checked) { // indicate found radio button has value anyradiobuttonhasvalue = true; // break out of each loop homecoming false; } }); // check if found radio button has value if (anyradiobuttonhasvalue) { // enable submit button. $("input[name='submitbtn']").removeattr("disabled"); } });

also, sake of thoroughness, here submit button:

<input id="submitbtn" name="submitbtn" type="submit" value="drill down" disabled="disabled" />

thanks much!

event delegation (also, utilize .prop() when removing disabled property submit button)

$("#radiogroupby").on("change", ":radio[name=group]", function() { var $radiobuttons = $(":radio[name=group]"); var anyradiobuttonhasvalue = false; // iterate through radio buttons $radiobuttons.each(function () { if (this.checked) { // indicate found radio button has value anyradiobuttonhasvalue = true; // break out of each loop homecoming false; } }); // check if found radio button has value if (anyradiobuttonhasvalue) { $("input[name='submitbtn']").prop("disabled", false); } });

javascript jquery html razor

No comments:

Post a Comment