如何更新具有匹配选项值文本的选择文本

How to update select text with matched option value text

本文关键字:文本 选项 选择 何更新 更新      更新时间:2023-09-26

我想在页面加载后使用选定的选项文本更新选择文本。我在单击选项并在页面加载后从浏览器获取 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
});

您没有正确连接。此外,您正在使用text的吸气剂。相反,请使用二传手。同样从您的评论中,我想您需要

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

试试这个伙伴>我想你正在尝试从选择框中选择选项

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