首页 > 解决方案 > 如何使用 js 更改选择选项值的内容

问题描述

我不擅长 javascript,但我试过这个......我试图让用户在搜索项目后切换到另一个页面。例如,用户将在汽车部分搜索梅赛德斯,然后选项值可以更改为梅赛德斯链接页面以及可以更改为 GMC 链接页面是用户搜索 gmc。这段代码似乎不起作用。下面是代码:

<div class="all" id="note">
    Some content will be posted here...
</div>
<select id="marketing" onChange="window.open(this.value)" multiple>
  <option value="1">cars</option>
  <option value="2">houses</option>
  <option value="3">Promotion</option>
</select> 

<script type="text/javascript">
  var cars = ['mercedes']
  $('#marketing').change(function(){
  if($(this).val() == '1'){
    if cars[0] ==='mercedes';{
      $(this).val() == "http://www.cars.com/mercedes";
    }
    }
  });

标签: javascriptjqueryselect

解决方案


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
var cars = ['mercedes']
    function myFunction() {
        var x = document.getElementById("marketing").value;
        var companey = document.getElementById('companies').value;
        console.log(x + companey);
        if (x == 1 && companey == "mercedes") {
            var url = 
            window.open("http://www.cars.com/mercedes" , '_blank');//for new tab
            //for same tab
            //window.open('http://www.cars.com/mercedes');
        }
    }
    </script>
</head>
<body>
    <!this is a working sample hope this will help>
    <div class="all" id="note">
    Some content will be posted here...    <br />
        <input type="text" id="companies">mercedes</input>

</div>
<select id="marketing" onchange="myFunction()">
    <option value="0"></option>
  <option value="1">cars</option>
  <option value="2">houses</option>
  <option value="3">Promotion</option>
</select> 
    
</body>
</html>

推荐阅读