jQuery if children have value, make them selected
I have a PHP value called per_page which I'm carrying on to a jQuery function.
I want the jQuery function to search the <select id="thisForm"> element for any <option> with the text() value equal to per_page.
I then want the function to make that <option> selected, so it appears like this: <option selected>.
I didn't know how to use this() or whatever once I've found the text() value.
Solution:
$(document).ready(function() {
var i = 'whatever your per_page valu开发者_StackOverflow中文版e is';
$("#changeForm option[value=" + i +"]").attr("selected",true);
});
I think that this should work:
$('#thisForm option:contains("per_page")').attr('selected',true);
Using contains().
JS Fiddle demo of concept.
加载中,请稍侯......
精彩评论