jQuery事件无缘无故地触发两次 [英] Jquery event triggering twice for no reason

查看:95
本文介绍了jQuery事件无缘无故地触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个简单的表单问题,当有人键入内容时,我试图触发ajax请求.调试后,当事件触发时,我会收到警报,但无论浏览器如何,它都会显示两次. (忽略输入标签中的"rel"属性,这是别的,除非您认为它的rel属性引起了问题).

I'm having a problem with a simple form where i'm attempting to fire an ajax request when someone types something. After debugging I'm left with an alert when the event is fired but it shows up twice, irrespective of the browser. (Ignore the 'rel' attribute in the input tags, that's for something else, unless you think its the rel attribute causing the issue).

<form class="formBig" name="formRegister" id="formRegister" method="post" action="">
    <label class="label" id="locationLabel" for="location">Location</label>
    <input class="input" name="location" id="location" type="text" rel="locationLabel" value="" />
</form>

<script type="text/javascript">
$("#location").click(function() {
    alert($(this).attr('id'));
});
</script>

推荐答案

我最终弄清楚了,这是因为(出于某种未知的原因)我所包含的jquery放在了表单之后.

I ended up figuring it out, it was because (for some unknown-to-me-reason) the jquery I was including was put in after the form.

但是,如果我将其包含在document.ready中,它可以正常工作.我认为这与DOM有关.我不知道,关于jQuery,我还是很满意.

However, if I included it within the document.ready, it worked fine. I assume this has something to do with DOM. I dunno, I'm still fairly n00bish when it comes to jquery.

准备就绪的文档通过脚本include从头标中调用.

And the document ready is called from within the head-tags via a script include.

这篇关于jQuery事件无缘无故地触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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