Javascript 使用 onclick in href 更改选择列表的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7538435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change value of a select list using onclick in href
提问by Ajay
I want to change the select box value using the onClick function of .
我想使用 .onClick 函数更改选择框值。
<form>
<select id="action">
<option value="a">Add</option>
<option value="b">Delete</option>
</select>
</form>
<a href="#" onClick="">Add</a>
<a href="#" onClick="">Delete</a>
回答by Bajrang
<form>
<select id="action">
<option value="a">Add</option>
<option value="b">Delete</option>
</select>
</form>
<a href="#" onClick="document.getElementById('action').value='a'">Add</a>
<a href="#" onClick="document.getElementById('action').value='b'">Delete</a>
OR U can also call this Java Script function for doing this =
或者你也可以调用这个 Java Script 函数来做这个 =
function changeval()
{
if(document.getElementById('action').value =='b')
document.getElementById('action').value='a'
else
document.getElementById('action').value='b'
}
<a href="#" onClick="changeval()">Add</a>
<a href="#" onClick="changeval()">Delete</a>
回答by Bas van Dijk
I think this is where you are looking for:
我想这就是你要找的地方:
<script type="text/javascript">
function changeValue(selectBox, value) {
selectedItem = selectBox[selectBox.selectedIndex];
selectedItem.value = value;
}
</script>
In the onclick you place"
在点击你的地方“
changeValue(document.getElementById("action"), "your value");
from: http://www.webdeveloper.com/forum/showthread.php?t=160118
来自:http: //www.webdeveloper.com/forum/showthread.php?t=160118
回答by deepi
<form>
<select id="action">
<option value="a">Add</option>
<option value="b">Delete</option>
</select>
</form>
<a href="#" onClick="action.value='a'">Add</a>
<a href="#" onClick="action.value='b'">Delete</a>
回答by jmespinosa
I used a jquery .click
and gave an id to the select element
This is the html code
我使用了 jquery.click
并为 select 元素提供了一个 id 这是 html 代码
<select id="carsList">
<option value="volvo" id="#volvoOption">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi" id="#audiOption">Audi</option>
</select>
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRgoru_AtWqrY6DWtmVlOvtYoxpdGHlheWUKgn0jpy0R7Z2VjjjDBh78cx1" id="volvoLogo">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQBKzqqECaAC5n-cij4sToayeEAjQqAzYcIrRDj1MeP5vo5OlUCvq_CPWHx" id="audiLogo">
And this is the jquery code I used
这是我使用的 jquery 代码
$( document ).ready(function() {
$("#volvoLogo").click(function(){
$("#carsList option[value='volvo']").attr('selected', 'selected');
});
$("#audiLogo").click(function(){
$("#carsList option[value='audi']").attr('selected', 'selected');
});
});