I am having an issue trying to get a hold of a cascading combo box on the outer combo box change event. you can add and remove rows from a table so there could be any number of rows. Inside each row are 2 combo boxes. 1 is for customer and 1 is for contact. I have a change event firing on customer combo box that goes and pulls the valid contacts for that customer. Below is the html for the table
<tr class="ClassAttendee">
<td>
<select class="Customers valid" id="Attendees_0__Customers"
name="Attendees[0].Customers">
<option value="1517">Woodstock</option>
<option value="1518">Woolwich</option>
<option value="1519">Yarmouth</option>
<option value="1520">York</option>
</select>
</td>
<td>
<select class="Contact">
<option value="">Choose Attendee...</option>
</select>
</td>
<td>
<input data-val="true" data-val-required="The Attended field is required."
id="Attendees_0__Attended" name="Attendees[0].Attended" type="checkbox"
value="true"><input name="Attendees[0].Attended" type="hidden" value="false">
<span class="field-validation-valid" data-valmsg-for="nestedObject.Attended" data
-valmsg-replace="true"></span>
</td>
<td>
<input class="FeePaid" data-val="true" data-val-required="The Fee Paid field is
required." id="Attendees_0__FeePaid" name="Attendees[0].FeePaid" type="checkbox"
value="true"><input name="Attendees[0].FeePaid" type="hidden" value="false">
<span class="field-validation-valid" data-valmsg-for="nestedObject.FeePaid" data
-valmsg-replace="true"></span>
</td>
<td>
<input class="CheckNumber" id="Attendees_0__CheckNumber"
name="Attendees[0].CheckNumber" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="nestedObject.CheckNumber"
data-valmsg-replace="true"></span>
</td>
<td>
<input class="mark-for-delete" data-val="true" data-val-required="The Delete field
is required." id="Attendees_0__Delete" name="Attendees[0].Delete" type="hidden"
value="False">
<a href="#"
onclick="javascript:removeNestedForm(this,'tr.ClassAttendee','input.mark-for
-delete');return false;">Remove</a>
</td>
</tr>
Each row looks like this where the first 2 cells contain drop downs. The first for Customer and the second for Contacts. If I just use a selector of $(“Contact”) I can update the combo boxes with the correct options so I know this works but I need to specify the row because when I just use that selector all rows get updated.
?Below is the JQuery I am trying to use
$('.Customers').change(function (e) {
var selectedCustomer = $(this).val();
if (selectedCustomer != null && selectedCustomer != '') {
$.getJSON('@Url.Action("GetAllSelectedCustomer", "CustomerEmails")', { id: selectedCustomer }, function (contacts) {
var contactSelect = $(this).parent().next().children(".Contact");
contactSelect.empty();
$.each(contacts, function (index, contact) {
contactSelect.append($('<option/>', {
value: contact.ID,
text: contact.Name
}));
});
});
}
});
The speicif line I am having issues with is
var contactSelect = $(this).parent().next().children(".Contact");
If I use a selector to first select the customer drop down box instead of using this and then use the rest of the line I can select the contacts drop down. I don’t understand why this is not working and I also have no ideas on how to select the specific drop down list on the row I am dealing with for the change event if I can’t use the this object.
Any ideas would be greatly appreciated.
Try using a closure: