简体   繁体   English

选项选择onchange

[英]option select onchange

This might be a primitive question, but I am new to JS/jQuery. 这可能是一个原始问题,但我是JS / jQuery的新手。

I have several different select boxes on a page. 我在页面上有几个不同的选择框。 Each of them has an image next to, that is changed, when a different option is selected. 当选择了不同的选项时,它们中的每一个都有一个图像,即更改的图像。 However, this only works fine for one select box with getElementById, but it doesn't work with getElementByClassName for many of them, nor do I want to use this method, since I read it's not supported by IE8.0. 但是,这对于带有getElementById的一个选择框只能正常工作,但对于其中许多选项,它不适用于getElementByClassName,我也不想使用此方法,因为我读过IE8.0不支持它。 Is there a way to make this work for multiple select boxes within one page, without using separate IDs for each one of them? 有没有办法让这个工作在一个页面内的多个选择框中,而不为每个选择框使用单独的ID?

Your help would be greatly appreciated. 非常感谢您的帮助。 Here's my code. 这是我的代码。

<img id="color" src="content/1.png"> 
<select class="mod_select" name="colors" id="changingColor" tabindex="1" onchange="changeimg()">
<option value="content/1.png">1 thing</option>
<option value="content/2.png">2 thing</option>
<option value="content/3.png">3 thing</option>
</select>

<script type="text/javascript">
function changeimg(){
document.getElementById('color').src=document.getElementById('changingColor').value
}
</script>

Edit: I'd like to use classes both for select and for img. 编辑:我想使用select和for img的类。 Then within one div have a particular select option change the img next to it. 然后在一个div中有一个特定的选择选项,改变它旁边的img。 Is that possible? 那可能吗?

Edit 2: This works great: 编辑2:这很有用:

$('.mod_select').on('change', function () {
     var $this = $(this);
     var img = $this.prev(); // assuming select is next to img
     img.attr('src', $this.val());
 });

However, the img is not next to select, I think I should use prevSibling, but I'm not sure how. 但是,img不是选择的旁边,我想我应该使用prevSibling,但我不确定如何。 Reading jQuery API didn't help me much. 阅读jQuery API并没有多大帮助。 I would really appreciate your help! 我将衷心感谢您的帮助! Here's my html: 这是我的HTML:

 <img src="content/1.png"> <!-- the image that needs to be updated with new select option -->
 <p>text</p>
 <div class="moreinfo info_icon left"></div> <!-- the div that triggers qTip -->
 <div class="moreinfo_text"> <!-- this is a hidden div this that is shown by qTip with -- text: $(this).next('.moreinfo_text') -->
      <img src="content/qtip_img.jpg">
      <p>qTip text</p> 
 </div> 
 <select class="mod_select" name="colors" tabindex="1">
      <option value="content/1.png">1 thing</option>
      <option value="content/2.png">2 thing</option>
      <option value="content/3.png">3 thing</option>
 </select>

See if this works for you please: 看看这是否适合您:

jQuery(document).ready(function($){

   $('.mod_select').change(function() {
    $('.mod_select option:selected').each(function() {
     $('#color').attr('src', $(this).val());
    });
   });

});    

You should add something like 你应该添加类似的东西

<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">/* <![CDATA[ */
!window.jQuery && document.write('<script type="text/javascript" src="/js/jquery.js"><\/script>')
/* ]]> */</script>

to the to add jQuery. 添加jQuery。

Since jQuery provides a very powerful selector, you can use a class selector to achieve your goal like the following: 由于jQuery提供了一个非常强大的选择器,您可以使用类选择器来实现您的目标,如下所示:

  1. Remove onchange attribute from select. 从select中删除onchange属性。
  2. Edit your javascript. 编辑你的JavaScript。

     $('.mod_select').on('change', function () { var $this = $(this); var img = $this.prev(); // assuming select is next to img img.attr('src', $this.val()); }); 

Since IE 8 doesn't support getElementsByClassName you can create your own version of it using the getElementsByTagName that Internet Explorer does support. 由于IE 8不支持getElementsByClassName您可以使用Internet Explorer支持的getElementsByTagName创建自己的版本。 Once you have the array of elements you can iterate over them and assign an event handler for the change event instead of having to do it inline with the html. 获得元素数组后,您可以迭代它们并为change事件分配事件处理程序,而不必与html内联。 Once this is done, all you need to do is find the image element by its class name using previousSibling or nextSibling and change its source. 完成此操作后,您需要做的就是使用previousSiblingnextSibling按类名查找image元素并更改其源。

Here is the javascript 这是javascript

var select = getElementsByClassName(document.body,'mod_select');
for (var i = 0; i < select.length; i++){
    select[i].onchange = function() {
      var prevSibling = this.previousSibling;
      while(prevSibling && prevSibling.className != 'image') {
           prevSibling = prevSibling.previousSibling;
      }
      prevSibling.src = this.value;
    }
}

function getElementsByClassName(node, classname) {
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName('*');
    for(var i = 0, j = els.length; i < j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

Here is a link to a fiddle showing this. 这是一个显示这个的小提琴的链接。 Note: this does not require any jQuery :) 注意:这不需要任何jQuery :)

edit 编辑

jQuery does all of the heavy lifting for you and you can change the above code to simply jQuery为您完成所有繁重工作,您可以将上述代码更改为简单

$('.mod_select').change(function(){
    $(this).prevAll('img:first').attr('src', this.value); 
});

This looks among all the previous sibling elements for the first image it comes to and changes its source. 这看起来是第一个图像的所有先前兄弟元素,并改变了它的来源。 You can change the selector to include the class name you mentioned you wanted to add in your first edit. 您可以更改选择器以包含您在第一次编辑中添加的类名。 Here is a fiddle showing the code with jQuery. 这是一个用jQuery显示代码的小提琴

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

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