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 如何根据查询中的ID更改SelectBox的值_Javascript_Jquery - Fatal编程技术网

Javascript 如何根据查询中的ID更改SelectBox的值

Javascript 如何根据查询中的ID更改SelectBox的值,javascript,jquery,Javascript,Jquery,我有几个selectbox,我想根据查询中的id更改它们的默认选项。我希望此操作在刷新整个页面时保留所选选项。我编写了javascript代码,但它不能正常工作,在某些情况下,它会为SelectBox返回错误或空值。 如何改进代码以在页面刷新后保持选中选项? 这是我的密码: document.getElementsByName(“大陆1”)[0]。值=[###cms.query.continold1#] document.getElementsByName(“大陆2”)[0].value=[#

我有几个selectbox,我想根据查询中的id更改它们的默认选项。我希望此操作在刷新整个页面时保留所选选项。我编写了javascript代码,但它不能正常工作,在某些情况下,它会为SelectBox返回错误或空值。 如何改进代码以在页面刷新后保持选中选项? 这是我的密码:

document.getElementsByName(“大陆1”)[0]。值=[###cms.query.continold1#]
document.getElementsByName(“大陆2”)[0].value=[###cms.query.continold2#]

第一大洲
亚洲
非洲
美国


第二大洲 Euroupe 澳大利亚 阿西萨
使用此代码保存url中存储的get参数,然后设置值:

Javascript

    var options = window.location.search.slice(1)
              .split('&')
              .reduce(function _reduce (/*Object*/ a, /*String*/ b) {
                   b = b.split('=');
                   a[b[0]] = decodeURIComponent(b[1]);
                   return a;
              }, {});

    document.getElementsByName("continent1")[0].value=[options.continent1];
    document.getElementsByName("continent2")[0].value=[options.continent2];

请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。请参阅“如何询问”页面,以获得澄清此问题的帮助。什么是[##cms.query.contricent1]@ZakariaAcharki模板引擎ithink@ZakariaAcharki我的地址栏中有大陆1。例如,在我的addressbar@inaz你能粘贴完整的网址吗?非常感谢!