简体   繁体   English

在img点击更改选择框选择

[英]Change selectbox selection on img click

When someone clicks a specific link on my website I want the select box option to change to the option to a certain option by the value it has set. 当有人点击我网站上的特定链接时,我希望选择框选项通过其设置的值更改为某个选项的选项。

I imagine I would do this by adding some sort of onClick that points to the value on each selection... but can't figure out the code to use. 我想我会通过添加某种指向每个选择的值的onClick来做到这一点......但是无法弄清楚要使用的代码。

In regular JavaScript you could do something similar to: 在常规JavaScript中,您可以执行类似于以下操作:

var selectBox = document.getElementById('selectBox');
var image = document.getElementbyId('someImage');

image.addEventListener('click', function() {
  selectBox.selectedIndex = 1; // specify the selected index here.
});

where you get a reference to the select box and image and add an event handler to the image to set the selected index of the select box. 您可以在其中获得对选择框和图像的引用,并向图像添加事件处理程序以设置选择框的选定索引。

If you have jQuery available, you can do similar: 如果你有jQuery可用,你可以这样做:

$('img').click(function() {
  $('select').val('new value'); // specify the new value
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM