Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于url中的查询字符串选择的下拉列表值_Javascript_Jquery_Url_Select - Fatal编程技术网

Javascript 基于url中的查询字符串选择的下拉列表值

Javascript 基于url中的查询字符串选择的下拉列表值,javascript,jquery,url,select,Javascript,Jquery,Url,Select,关于这个问题,这里有一个类似的问题,但我似乎无法让它工作 因此,当点击链接时,url会将值传递给该页面上的下拉列表,以便预先填充 <script type="text/javascript"> $(document).ready(function () { $('#idOptions').on('change', function () { window.location.assign('/string.html?strings=' + $(this)

关于这个问题,这里有一个类似的问题,但我似乎无法让它工作

因此,当点击链接时,url会将值传递给该页面上的下拉列表,以便预先填充

    <script type="text/javascript">
$(document).ready(function () {
    $('#idOptions').on('change', function () {
        window.location.assign('/string.html?strings=' + $(this).val());
    });
});
</script>

<select id="idOptions">
        <option>option1</option>
        <option>option2</option>
        <option>option3</option>
        <option>option4</option>
        <option>option5</option>
</select> 

$(文档).ready(函数(){
$('#idOptions')。关于('change',函数(){
window.location.assign('/string.html?strings='+$(this.val());
});
});
选择1
选择2
选择3
选择4
选择5
那么下面的链接就在另一个页面上了

string.html?strings=option1

基于此示例

就像这样

在更改或单击事件时,根据需要进行更改

document.location.href = "http://someurl" + $("#idOptions").val();

window.location = "http://someurl" + $("#idOptions").val();
两件事:

  • 当您需要所选选项值时,您的代码将获得选择值
  • 您的选项没有值,因此您必须获取选项的文本
  • 以下是您需要的代码:

    $('#idOptions').on('change', function () {
        window.location.assign('/string.html?strings=' + $(this).find('option:selected').text());
        // window.location.assign('/string.html?strings=' + $(this).find('option:selected').val()); // Use this one to get the option value instead of the text
    });
    

    所选的
    选项的值或文本是
    选择的值:确定我现在向选项标签添加了值,使用val()我的链接正确吗?我使用string.html?strings=option2