jquery onclick 函数未定义 [英] jquery onclick function not defined

查看:47
本文介绍了jquery onclick 函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ajax 脚本,我正在尝试从一个函数中发帖.我正在使用 onlick href 但它没有出现未定义.这是使用wordpress.我试图在范围内外移动代码,但我似乎仍然无法让它工作.

 

<div class="容器"><?php the_content();?><div id="评论显示"><form method="post" action="index.php" id="comments_submit"><input type="hidden" id="nameBox" value="<?php echo $_SESSION['name'] ?>"名称=名称"/><input type="hidden" id="emailBox" name="email" value="<?php echo $_SESSION['email']; ?>"/><textarea id="chatBox" placeholder="提出问题或发表评论" name="comment" class="form-control"></textarea><a href="javascript:submitComment();"type="submit" id="submit" name="submit" class="btn cardh-bg text-white text-bold margin-top-5">提交评论</表单><br/><div id="displayComments"></div>

<script type="text/javascript">jQuery(函数($){设置间隔(函数(){$.ajax({方法:获取",url: "<?php echo get_template_directory_uri()?>/get_chat.php"}).完成(功能(html){$('#displayComments').html(html);});}, 2000);功能提交评论(){$.ajax({方法:POST",网址:模板-live.php",数据:{submitComment:$('#chatBox').val(),submitName:$('#nameBox').val(),submitEmail:$('#emailBox').val()}}).完成(功能(html){alert('您的评论已提交,审核通过后显示.');$('#chatBox').val('');});}});

谢谢:)

解决方案

当您执行 javascript:submitComment() 时,会调用全局函数 submitComment.由于 submitComment 是在 jQuery(function($) { ... }) 函数中定义的,所以它不是全局的.因此,window.submitCommentundefined(因此 undefined 不是函数).

全局变量存储在 window 对象中.

因此,您可以将该 submitComment 公开为全局:

window.submitComment = function () {...}

请注意,您应该尽可能避免使用全局变量.在这种情况下,您可以通过添加:

$("#submit").click(submitComment);//在这种情况下,您不应再将 submitComment 声明为全局变量

并且由于您在表单中,因此您希望在单击 a 元素时停止默认浏览器行为,方法是在函数末尾使用 return false.

I have an ajax script and I am trying to post from a function. I am using a onlick href but its not coming up as undefined. This is using wordpress. I have tried to move the code around inside and outside the scope but I still cant seem to get it to work.

    <div id="live">
    <div class="container">
        <?php the_content(); ?>
        <div id="comment-display">
            <form method="post" action="index.php" id="comments_submit">
                <input type="hidden" id="nameBox" value="<?php echo $_SESSION['name'] ?>" name="name"/>
                <input type="hidden" id="emailBox" name="email" value="<?php echo $_SESSION['email']; ?>"/>
                <textarea id="chatBox" placeholder="Ask a question or make a comment" name="comment" class="form-control"></textarea>
                <a href="javascript:submitComment();" type="submit" id="submit" name="submit" class="btn cardh-bg text-white text-bold margin-top-5"> Submit Comment </a>
            </form>
            <br />
            <div id="displayComments"></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    jQuery(function($) {
        setInterval(function(){
            $.ajax({
                method: "GET",
                url: "<?php echo get_template_directory_uri()?>/get_chat.php"
            }).done(function(html){
                $('#displayComments').html(html);
            });
        }, 2000);

        function submitComment(){
            $.ajax({
                method: "POST",
                url: "template-live.php",
                data: {submitComment:$('#chatBox').val(),submitName:$('#nameBox').val(),submitEmail:$('#emailBox').val()}
            }).done(function(html){
                alert('Your comment has been submitted, and will be displayed after approval.');
                $('#chatBox').val('');
            });
        }
    });
</script>

Thank you :)

解决方案

When you do javascript:submitComment() that's calling a the global function submitComment. Since the submitComment is defined in the jQuery(function($) { ... }) function, it is not a global. Therefore, window.submitComment is undefined (hence undefined is not a function).

The globals are stored in the window object.

Therefore, you can expose that submitComment as a global:

window.submitComment = function () {...}

Note that you should avoid using globals as much as possible. In this case you can do that by adding:

$("#submit").click(submitComment);
// In this case, you shouldn't declare submitComment as a global anymore

And since you are in a form, you want to stop the default browser behavior when clicking the a element, by using return false at the end of the function.

这篇关于jquery onclick 函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆