当有人点击我网站上的特定链接时,我希望选择框选项按照其设置的值更改为某个选项。
我想我会通过添加某种指向每个选择上的值的 onClick 来做到这一点......但无法弄清楚要使用的代码。
最佳答案
在常规 JavaScript 中,您可以执行类似以下操作:
var selectBox = document.getElementById('selectBox');
var image = document.getElementbyId('someImage');
image.addEventListener('click', function() {
selectBox.selectedIndex = 1; // specify the selected index here.
});
您可以在其中获取对选择框和图像的引用,并向图像添加事件处理程序以设置选择框的选定索引。
如果您有可用的 jQuery,您可以执行类似的操作:
$('img').click(function() {
$('select').val('new value'); // specify the new value
});
关于javascript - 更改图像单击上的选择框选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051377/