简体   繁体   中英

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.

In regular JavaScript you could do something similar to:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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