简体   繁体   English

单击内部单击功能导致多次显示警报消息 - Jquery / Javascript

[英]Click inside click function causing alert message showing more than once - Jquery/Javascript

My question is little hard to explain so i have made a jsfiddle . 我的问题很难解释,所以我做了一个jsfiddle The problem is that. 问题是。 when i click on <li> first time, i get 1 alert message. 当我第一次点击<li>时,我收到1条提醒信息。

Now i again click on any <li> , now what i am getting 2 alerts. 现在我再次点击任何<li> ,现在我得到2个警报。

First click on any input field --> then li --> then input field again --> then li again. 首先单击任何input field --> then li --> then input field again --> then li again.

Markup: 标记:

<input type='text' class='InputField'>
<input type='text' class='InputField2'>
<input type='text' class='InputField3'>
<div class="ranges FullBoxControl" style='display:none'>
    <ul>
        <li>This Month</li>
        <li>This Year</li>
        <li>Last 5 Years</li>
        <li>Last 10 Years</li>
        <li>Custom Range</li>
    </ul>
</div>

Script code: 脚本代码:

$(".InputField").click(function()
{
    $('.FullBoxControl').show();
    $('.FullBoxControl').click(function()
    {
        alert('Greetings');
    });
});

$(".InputField2").click(function()
{
    $('.FullBoxControl').show();
    $('.FullBoxControl').click(function()
    {
        alert('Greetings');
    });
});

$(".InputField3").click(function()
{
    $('.FullBoxControl').show();
    $('.FullBoxControl').click(function()
    {
        alert('Greetings');
    });
});

JsFiddle Demo

Ask me if you people didn't get it. 问我是否有人没有得到它。

Use unbind to remove the double event handler 使用unbind删除double事件处理程序

$(".InputField").click(function()
{ 
    $('.FullBoxControl').show();
    $('.FullBoxControl').unbind();
    $('.FullBoxControl').click(function()
    {
        alert('Greetings');
    });
});

Fiddle 小提琴

Refer jsfiddle . 请参考jsfiddle Each time you click in input field, it binds click event, so it is necessary to put repetitive code in common function and unbind click event, before binding click event again. 每次单击输入字段时,它都会绑定click事件,因此在重新绑定click事件之前,有必要将重复代码放在常用函数中并取消绑定click事件。

$(".InputField").click(function()
{
    fullbox();
});

$(".InputField2").click(function()
{
    fullbox();
});

$(".InputField3").click(function()
{
    fullbox();
});
function fullbox(){
    $('.FullBoxControl').show();
    $('.FullBoxControl').unbind("click");
    $('.FullBoxControl').click(function()
    {
        alert('Greetings');
    });
}

Set a flag to see if you have already attached the click event: 设置一个标志以查看您是否已附加click事件:

http://jsfiddle.net/x85A6/6/ http://jsfiddle.net/x85A6/6/

var eventAttached = false;

$(".InputField").click(function() {
    if (!eventAttached) {
        $('.FullBoxControl').show();
        $('.FullBoxControl').click(function(){
            alert('Greetings');
        });
        eventAttached = true;
    }
});

$(".InputField2").click(function() {
    if (!eventAttached) {
        $('.FullBoxControl').show();
        $('.FullBoxControl').click(function(){
            alert('Greetings');
        });
        eventAttached = true;
    }
});

$(".InputField3").click(function() {
     if (!eventAttached) {
        $('.FullBoxControl').show();
        $('.FullBoxControl').click(function(){
            alert('Greetings');
        });
        eventAttached = true;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM