【问题标题】:Dynamic Click via JavaScript (or jQuery) on a Checkbox通过 JavaScript(或 jQuery)在复选框上动态单击
【发布时间】:2010-07-09 21:27:29
【问题描述】:

我知道我可以将属性检查更改为检查,但我需要实际“单击”复选框以触发 live() (jQuery) 单击事件。我知道我也可以把它变成一个函数,但如果可能的话,这只会更简洁,代码更少。

哦,仅供参考:

$('.modal-form [name='+json[0].owners[x].name+']').click();

不起作用。它检查 em,但实际上并没有将它们“点击”到 jQuery 触发 live() 事件的位置

如果有人感兴趣,这里是 AJAX 调用:

$('.agenda-modal input[type=checkbox]:not([name="emergency-item"])').live('click',function(){
        iid = $('.agenda-modal').attr('data-defaultitemid');
        the_type = $(this).parent().parent().attr('id');
        checked_item = $(this).val();

        if($(this).attr('checked')){
            if(the_type == 'the-elected-list'){
                $livepreview.agenda({
                    action:'update',
                    type:'owner',
                    id:iid,
                    owner:checked_item
                });
            }
            else if(the_type == 'the-bureau-list'){
                $livepreview.agenda({
                    action:'update',
                    type:'bureau',
                    id:iid,
                    bureau:checked_item
                });
            }
        }
        else{
            $livepreview.agenda({
                action:'get',
                type:'item',
                id:iid,
                callback:function(json){
                    if(the_type == 'the-elected-list'){
                        for(x in json[0].owners){
                            if(json[0].owners[x].name == checked_item){
                                $livepreview.agenda({
                                    action:'delete',
                                    type:'owner',
                                    id:json[0].owners[x].item_owner_id
                                });
                            }
                        }
                    }
                    else if(the_type == 'the-bureau-list'){
                        for(x in json[0].bureaus){
                            if(json[0].bureaus[x].name == checked_item){
                                $livepreview.agenda({
                                    action:'delete',
                                    type:'bureau',
                                    id:json[0].bureaus[x].item_bureau_id
                                });
                            }
                        }
                    }
                }
            });
        }
    });

【问题讨论】:

  • 请举个例子。通过制作复杂的表达式,您很可能找不到正确的元素。把它调低。
  • @meder 好吧,如果它正在检查复选框,那么他正在找到正确的元素。
  • 不,就像我说的那样,在上面的代码中,它点击(并检查元素)但不点击元素......有一个 AJAX 调用(在上面 1 秒内发布)当单击复选框以即时保存选择时会触发它。
  • 您使用的是什么版本的 jQuery?我认为在 1.4 之前(实际上 1.4.2 是 1.4 的“固定”版本)事件冒泡有点不稳定
  • 嗯,一个非常简单的测试页面表明它可以工作 - 是不是某个父元素上有一个“click”处理程序,它返回“false”?

标签: javascript jquery html forms checkbox


【解决方案1】:

这将“单击”复选框。我不确定它是否会触发与 live() 绑定的事件。

$('#myCheck').click();

【讨论】:

    【解决方案2】:

    请举个例子。通过制作复杂的表达式,您很可能找不到正确的元素。把它调低。

    var el = $('.modal-form [name='+json[0].owners[x].name+']')
    alert( el.length )
    

    那你可以.trigger('click')它。确保事件处理程序已绑定/设置。

    【讨论】:

    • 对于每个项目,我都会在警报中返回一个“1”。因此,如果我选择了两个“所有者”,我会收到 2 个警报,每个警报都返回“1”。
    【解决方案3】:

    显然我不知道您的代码库,但我真的想知道通过伪造 UI 交互来调用函数是否会“干净得多”。它的代码可能更少,但对我来说听起来很脆弱。重新设计 UI 时会发生什么?或者其他开发人员修改了live() 事件处理程序,却没有意识到您的代码也在隐式使用它?将代码移动到单独的函数中使其更易读、更健壮(并且可能不会显着延长)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      相关资源
      最近更新 更多