javascript - JQuery Selector not working, where looping does. Why? -
i have 2 jquery selectors used did not find desired option third "brute force" method did. @ complete loss why. each looked should of , validated document loaded when selector ran. tried each value , text in selector , got same result in page. html dynamically loaded using jquery load method below (where thetype value coming html select control) associated javascript:
var thetype = $('#tickettype option:selected').val(); var theid = $("#id").val(); var url = '/systembuildsiteconfigs/' + thetype + '/' + theid; $('#ticketdiv').html(""); $("#ticketdiv").load(url, function (response, status, xhr) { if (status == "error") { alert("there error loading " + thetype + " form"); } else { $.getscript("/scripts/siteconfig" + thetype + ".js", function () { }); } });
the html select in question:
<select name="supportorganization" class="form-control" id="supportorganization"> <option value="">- please select -</option> <option>auxiliary it</option> <option>business intelligence</option> <option>change management</option> <option>engineering rnd</option> <option>enterprise business solutions</option> <option>enterprise solutions</option> <option selected="selected">hosting</option> <option>infrastructure , operations</option> <option>manufacturing</option> <option>professional</option> <option>support</option> </select>
first selector statement:
$("#supportorganization").find('option[value="' + jsonobj.supportorganization + '"]').attr("selected", true);
second selector statement:
$('#supportorganization option[value="' + jsonobj.supportorganization + '"]').attr("selected", true);
what worked:
$('#supportorganization option').each(function (idx, ele) { if ($(ele).val() == jsonobj.supportorganization) $(ele).attr("selected", true); });
the page segment loads correctly , other javascript fires load html controls content executing expected. page type loads , fires correctly performing similar functions. using jsfiddle, both selectors show work correctly. found through testing was
- the variable did have expected value.
- the text selector did not work correctly on page
- the value selector did select object. unable determine object , attr() method did not set target attribute value.
i can go found work, wanted find out why wouldn't work. guessing has dynamically loaded html , javascript?
the jquery .val()
function uses "hook" <option>
elements return text content of element if "value" attribute missing. <option>
elements don't have "value" attributes, selector not match.
Comments
Post a Comment