Change to a option on click image with jQuery

烂漫一生 提交于 2019-12-23 04:49:23

问题


I would like to change option into select if I click on some image. Value of option is a unique ID of parameter. I would like to use jQuery but I'm beginner and I don't know how. Now I have this:

$(document).ready(function () {
  $('img').click(function(){
    $("option:selected").removeAttr("selected");
    $('option[value="' + $(this).attr("alt") + '"]').attr('selected', true);
  });
});

But it doesn't work. All source are here http://jsfiddle.net/EKaKw/2/. Answer into jsfiddle would be best. Thanks advance.


回答1:


the first, you havn't selected jquery library,

the second, there is an easier way to change selection in jquery

$(document).ready(function () {
    $('img').click(function(){
        $('select').val($(this).attr('alt'));
    });
});

fiddle: http://jsfiddle.net/73VmN/




回答2:


You have not included Jquery library.

$(document).ready(function () {
  $('img').click(function(){
    $("option:selected").removeAttr("selected");
     $('option[value="' + $(this).attr("alt") + '"]').attr('selected', 'selected');
 });
});


来源:https://stackoverflow.com/questions/18183391/change-to-a-option-on-click-image-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!