Jquery单击另一个元素并显示选择选项 [英] Jquery click on another element and display select options

查看:108
本文介绍了Jquery单击另一个元素并显示选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次拜访这个小提琴:



http:/ /jsfiddle.net/Y4LMX/



我想知道有没有办法转移点击?我的意思是,当我点击绿色区域(< div class =arrow/> )时,wi < select& ; 标签也会得到点击,或选择并显示下面的选项。是否有办法这样做?



注意:我这样做支持IE的< select>

  $( .ie-arrow)。click(function(){
$(this).prev()。find($(select)。click());
});

但是,它不工作。你能解决吗?



谢谢。

解决方案

从回答的想法不。这两个问题,这里是一个解决方案:访问此小提琴



javascript:

  $(。drop1)。append(< div onclick ='runThis1 'class ='arrow'>< / div>); 
$(。drop2)。append(< div onclick ='runThis2()'class ='arrow'>< / div>);

(function(){
showDropdown = function(element){
var event;
event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown',true,true,window);
element.dispatchEvent(event);
};

window.runThis1 = function(){
var dropdown = document.getElementById('dropdown');
showDropdown(dropdown);
};

window.runThis2 = function(){
var dropdown = document.getElementById('dropdown2');
showDropdown(dropdown);
};
})();


at 1st kindly visit this fiddle:

http://jsfiddle.net/Y4LMX/

I was wondering is there a way to transfer the click? I meant, when I click on the green area ( <div class="arrow"/> ) in the meantime the wi<select> tag also get clicked, or selected and shows the options bellow. Is there a way to do this?

NB: I am doing this for IE supported <select> tag.

I tried with the following code:

$(".ie-arrow").click(function(){
     $(this).prev().find($("select").click());
});

but, It's not working. Can you solve it?

Thanks.

解决方案

Okay, following the Idea from answer no. two of this question, here is a solution: Visit this fiddle

javascript:

$(".drop1").append("<div onclick='runThis1()' class='arrow'></div>");
$(".drop2").append("<div onclick='runThis2()' class='arrow'></div>");

(function () {
        showDropdown = function (element) {
            var event;
            event = document.createEvent('MouseEvents');
            event.initMouseEvent('mousedown', true, true, window);
            element.dispatchEvent(event);
        };

        window.runThis1 = function () {
            var dropdown = document.getElementById('dropdown');
            showDropdown(dropdown);
        };

        window.runThis2 = function () {
            var dropdown = document.getElementById('dropdown2');
            showDropdown(dropdown);
        };
    })();

这篇关于Jquery单击另一个元素并显示选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆