Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  114] [ 7]  / answers: 1 / hits: 6023  / 10 Years ago, thu, april 24, 2014, 12:00:00

My View code in MVC: I want to disable the Dropdownlist in View code when the checkbox is checked.



function SaveNewGroup() {

var group = RetrieveGroup();
var IsChecked = $(IsAssociation).is(:checked);
var url = (IsChecked) ? /Administration/SaveNewGroupforIsAssociation : /Administration/SaveNewGroup;
var userID = $('#groupunderwriter').val();

$.ajax({
type: POST,
url: url,
data: group,
datatype: json,
success: function (groupID) {
if (groupID > 0) {

GetGroups();

$('#groupdialog').dialog('close');
}
else {
alert(Unable to create Group.);
}
}
});
}


Check box:



<tr>
<td>
<label>Is Association</label></td>
<td>
<input type =checkbox id=IsAssociation/>
</td


>

and my dropdownlist:



<tr>
<td>Underwriter Name:</td>
<td>
<select id=groupunderwriter style=width:150px;>
<option value =0></option>
@foreach (RMS.UserService.User u in Model.GroupUnderWriters)
{
<option [email protected]>
@if(Model.MasterGroupAttribute.UserID == u.UserID)
{
@:selected=selected
}
>@(u.FirstName + + u.LastName )</option>

}
</select>
</td>

</tr>


How to disable the dropdownlist when the checkbox is checked or enable it when not checked?


More From » jquery

 Answers
23

Have a look at this fiddle. It works fine for me. i don't know why all the above are not working for you. I just used the jQuery ON event listener in case you have a race condition and these elements don't exist when you are creating a binding for them. Unlikely but hey, my example works. This isn't the ideal way to do it but it may give you some insight as to whatever is wrong with your code.



<input type =checkbox id=IsAssociation /><span>your checkbox</span>
<br/>
<br/>
<select id=groupunderwriter style=width:150px;>
<option value =0>Hello</option>
<option value =1>Goodbye</option>
</select>

$(document).on('change', '#IsAssociation', function(){
if($(this).prop('checked')){
$('#groupunderwriter').attr('disabled', 'disabled');
} else {
$('#groupunderwriter').removeAttr('disabled');
}
});


http://jsfiddle.net/T83vs/


[#45782] Wednesday, April 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;