javascript - Disable Dropdownlist based on Radio Button -
goal:
if select "dates", can select dropdownlist start date , end date. if select "all ... only" start , end date grey colored in background , cannot click on arrow down. these dropdownlists disable.
problem:
don't know how create in frontend code.
info:
*the dropdownlists created in asp.net mvc 4
*i'm using jquery 1.10 , bootstrap

<input id="aa" type="radio" name="searchselection" value="all" style="display: inline-block;" checked> <label for="aa" style="width: 100px; display: inline-block; ">all ...only</label> <input id="dates" type="radio" name="searchselection" value="dates" style="display: inline-block;"> <label for="dates" style="width: 100px; display: inline-block;">dates</label> @{ datetime mydate = datetime.today; list<selectlistitem> mylistselectlistitem_yearstartdate = new list<selectlistitem>(); (int = 0; < 10; i++) { mylistselectlistitem_yearstartdate.add(new selectlistitem { text = (mydate.year - i).tostring(), value = (i + 1).tostring(), selected = datetime.today.year == (mydate.year - i) ? true : false }); } } @html.dropdownlist("yearstartdate", mylistselectlistitem_yearstartdate)
this fixed in following jsfiddle
i've stripped out of unneeded html attributes (such style tags - styles better applied in css) , stubbed out end code generating <select> in order simplify example , focus on solution.
let's @ what's happened:
<select class='js-date-selector' disabled='disabled'> firstly, each of select elements has been edited add following 2 attributes. class allows targeting javascript (or jquery) - note js- prefix not essential, it's nice way of keeping javascript class attributes separate others. also, class used instead of id, best easier re-use, have in example.
the disabled attribute how mark-up html element it's greyed out. if you're going mark 'all dates' checked on page load , 'all dates' being checked means selects should disabled, html needs mark selects disabled on load.
next bit toggling:
$('.js-all-or-dates').on('click',function() { var justclicked = $(this), dateselectors = $('.js-date-selector'); if (justclicked.attr('id') === 'aa') { dateselectors.attr('disabled', true); } else { dateselectors.attr('disabled', false); } }); firstly, bind function click event each of our .js-all-or-dates radio inputs.
secondly, assign variables, using justclicked = $(this) store jquery version element clicked , dateselectors store of our select items, using class mentioned above
finally, @ clicked , if has id of 'all dates' radio input set disabled property on select elements.
also, practice , smoother development: === used equality; $ function calls minimised assigning results local variables; , var statement contains comma separated declarations.
Comments
Post a Comment