调用ajax调用返回的javascript代码中的.trigger("click") [英] calling .trigger("click") that is in javascript code returned from an ajax call

查看:237
本文介绍了调用ajax调用返回的javascript代码中的.trigger("click")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从ajax调用返回的视图中触发按钮的click事件.在PC浏览器(例如chrome)中,此过程可以正常运行,但在移动浏览器中则不能. (我同时使用jquery和jquery mobile).

What i would like to do is trigger a button's click event from within a view that gets returned from an ajax call. This process works fine in a pc browser such as chrome but not so in a mobile browser. (i am using both jquery and jquery mobile).

(涉及更多代码,但为清楚起见,我已将其删除)

(there is a lot more code involved in this but i have removed it for clarity)

我的html页面上有一个按钮,就像这样.

I have a button in my html page like so.

 <input type="button" id="bt1" value=""  />

在其他地方定义了onclick事件监听器.

It has an onclick event listener on it defined somewhere else.

我有一个ajax调用,它会像这样调用php脚本:

I have an ajax call that calls a php script like so:

 $.ajax({
        url: 'blah.php',
        type: 'get',
        data: {
            id : $('#field').val()
        },
        dataType: 'html',
        success: function (data) {
           $('#somediv').html(data);
        }
    });

它返回数据",它是带有内联javascript的html片段.它包含以下代码:

It returns "data" which is a segment of html with inline javascript. It containts the following code:

<div>
<script type="text/javascript">
    $(document).ready(function () {
      $("#bt1").trigger("click");
     });
</script>
</div>

我注意到,在手机中使用时不会触发触发事件.但在PC上工作正常.

What ive noticed is that the trigger event will not be fired when used in a mobile. but works fine in pc.

在处理移动设备时,DOM中是否存在其他差异,如果对象在视图外部,它会阻止触发器(点击")工作?

Is there a different in the DOM when dealing with mobile that it prevents a trigger("click") from working if the object is outside the view?

推荐答案

由于JavaScript代码和按钮元素是通过编程方式创建的,因此无法找到按钮,请尝试:

It cannot find the button since the javascript code and the button element is being created programatically, try:

$(document).find('#bt1').trigger('click');

$(document).find('#bt1').each(function(e) {
    e.trigger('click');
}

或者更好:

success: function (data) {
    $('#somediv').html(data).promise().done(){
       $('#bt1').trigger('click');
    });
}

这篇关于调用ajax调用返回的javascript代码中的.trigger("click")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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