2011-01-31 43 views
0

我有一個名爲per_page的PHP值,我正在執行一個jQuery函數。jQuery如果孩子有價值,讓他們選擇

我想讓jQuery函數在<select id="thisForm">元素中搜索任何<option>的文本()值等於per_page

然後我想要使該<option>被選中的功能,所以它看起來像這樣:<option selected>

我不知道如何使用this()或任何一旦我找到了text()的值。

解決方案:

$(document).ready(function() { 
    var i = 'whatever your per_page value is'; 
    $("#changeForm option[value=" + i +"]").attr("selected",true); 
}); 
+0

你爲什麼用javascript來做到這一點?爲什麼不使用PHP當它呈現頁面選擇正確的選項? – PetersenDidIt

回答

3

我認爲這應該工作:

$('#thisForm option:contains("per_page")').attr('selected',true); 

使用contains()

JS Fiddle demo的概念。