如何使用匹配的选项值文本更新选择文本

时间:2015-01-29 09:42:36

标签: javascript jquery

我想在页面加载后用选定的选项文本更新选择文本。 我点击一个选项重定向,然后页面加载从浏览器获取URL并检查哪个选项具有该值,并希望在选择文本中替换匹配选项的文本。

<div class="sorting-option">
   <select id="selectdropdown" class="dropdown-select">
    <option value="">Recommended Items</option>
    <option value="?sort1desc=F&sort1=Item_NAME">Name (A-Z)</option>
    <option value="?sort1desc=T&sort1=Item_NAME">Name (Z-A)</option>
    <option value=".f?sort1desc=F&sort1=Item_ONLINECUSTOMERPRICE">Price  
    (Low-High)   
    </option>
    <option value=".f?sort1desc=T&sort1=Item_ONLINECUSTOMERPRICE">Price  
    (High Low) 
    </option>
   </select>
 </div>

这就是我试过的

$(document).ready( function() {
$('#selectdropdown').bind('change', function () {
location.href = $(this).val();
});
var brwsr_url=document.URL;
var subSting = brwsr_url.substring(brwsr_url.indexOf('?')); //here im getting substring of url to match with option value
$('#selectdropdown').find('option[value="subSting"]').text(); // here im trying to replace the select text with option text which has that url substring
});

2 个答案:

答案 0 :(得分:0)

你没有正确连接。您也正在使用text的getter。相反,使用setter。同样来自你的评论,我想你需要

$('#selectdropdown').val(subSting);

答案 1 :(得分:0)

试试这个配偶&gt;我想你正试图从选择框中选择选项

$('#selectdropdown').find("option[value='"+subSting+"']").attr("selected","selected");
相关问题