Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery选择器有时返回空对象_Javascript_Jquery - Fatal编程技术网

Javascript jquery选择器有时返回空对象

Javascript jquery选择器有时返回空对象,javascript,jquery,Javascript,Jquery,如果单击的radiobutton的值为“不重要”或“对于未来”,我有一段代码将焦点放在textarea$newCommentBody字段上 $(document).ready(function () { $('#inhabitant_request_realization').on('click', 'input[type=radio]', function () { var $newCommentBody = $('.fos_comment_comment_form_h

如果单击的radiobutton的值为“不重要”或“对于未来”,我有一段代码将焦点放在textarea$newCommentBody字段上

$(document).ready(function () {
    $('#inhabitant_request_realization').on('click', 'input[type=radio]', function () {
        var $newCommentBody = $('.fos_comment_comment_form_holder textarea'),
            $tooltiptext = $('.tooltiptext'),
            $this = $(this);

        if ($this.val() == 'not_important' || $this.val() == 'for_the_future') {
                $newCommentBody.focus();
                $tooltiptext.show();
        } else {
                $tooltiptext.hide();
        }
    });
});
问题是,$newCommentBody是异步加载的,单击单选按钮后,焦点有时会放在textarea上,有时不是因为在这种情况下$newCommentBody是空的


谢谢。

如果您正在通过ajax加载$newCommentBody,则在成功对象上添加click事件监听器,则可能会重复。所以直到物品装好它才开始听。谢谢你!我使用了来自的ajaxStop事件,它解决了我的问题!