如何手动单击一个jQuery多选框?

问题描述

| 我在页面上有几个多选框。当用户单击某个复选框时,我希望页面上的多个选择框之一自动以某个值被单击。您对如何执行此操作有任何想法吗? 谢谢!
$(\"#select_country\").multiselect({
  multiple: false,header: \"Select a country\",noneselectedText: \"Select a country\",selectedList: 1
});
====== 到目前为止,我已经尝试了以下方法,但均未成功:
$(\"#select_country\").multiselect(\"widget\").find(\"input:checkBox\").each(function(){
  this.click();
 }); 

$(\"#select_country\").multiselect(\"widget\").find(\"input:checkBox\").triggerHandler(\'click\');

$(\"#select_country\").trigger(\'click\');
    

解决方法

        看看是否有帮助(演示@ http://jsfiddle.net/ehynds/PKypd/):
var second = $(\"#select_something\").multiselect();

$(\"#select_country\").multiselect({
    multiple: false,header: \"Select a country\",noneSelectedText: \"Select a country\",selectedList: 1,click: function( event,ui ) {

        // if the user clicks \"usa\",check the first box
        // in the second select.  if the user selects \"canada\",// check the second box.
        //
        // the call to filter() ensures that the items from the
        // second select remain selected if the user chooses
        // the same item from the first select multiple times.
        //
        // you can easily swap out .eq() for another filter()
        // and to choose checkboxes by value instead of their
        // position.
        second.multiselect(\'widget\')
            .find(\':checkbox\')
            .eq(ui.value === \'usa\'? 0 : 1)
            .filter(\':not(:checked)\')
            .each(function(){
                this.click();
            });
    }
});
同样,不要忘记multiselect只是对基础选择框的视觉增强。您在multiselect中所做的任何更改也会在select元素本身上进行,反之亦然。因此,您不必费心弄清楚如何在多选中做到这一点。您可以在原始选择框中选择一个选项标签,然后调用ѭ3告诉小部件刷新自身。 关于在DOM节点上调用
.click()
,而不是ѭ5或
$.fn.click()
,您是正确的。原因是由于jQuery中的此错误,在该错误中,trigger(\“ click \”)在复选框本机click()方法之前调用处理程序。     ,        
$(\"#select_country\").multiselect(\"widget\").find(\":radio[value=\'PUT_YOUR_VALUE\']\").each(function() {
     this.click();
});
$(\"#select_country\").multiselect(\'refresh\');