0

当有人单击我网站上的特定链接时,我希望选择框选项通过其设置的值更改为某个选项的选项。

我想我会通过添加某种指向每个选择的值的 onClick 来做到这一点......但无法弄清楚要使用的代码。

4

1 回答 1

2

在常规 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
});
于 2013-02-24T11:46:11.187 回答