javascript - 如何使用 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";
}
}
});
解决方案
<!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>
推荐阅读
- sql - 已解决:TYPO3 控制器:批量插入时“字段列表”中的未知列“uid”
- python - How to find the principal axis of a blob which goes through the centroid of the blob?
- flutter - Flutter Dart:无法加载 SVG 资产:无法加载资产图标路径
- javascript - 关于反应上下文和重新渲染组件的快速问题
- javascript - 如何在 Angular Material 11 的 MatSlideToggle 中使用图标?
- html - CSS 高度属性不适用于 h1 元素
- php - 从数组创建分页
- javascript - 如何导航和设置角度下拉列表中检查的值
- javascript - 如何在编辑时隐藏输入字段的中间部分?
- botframework - 通过机器人框架将气泡发送到团队会议中