如何使用jquery live函数提交点击事件(div)? [英] How do one only submit click event (div), when using jquery live function?

查看:85
本文介绍了如何使用jquery live函数提交点击事件(div)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery live函数提交点击事件(div)?



我在做什么。



点击输入并在mysql中搜索联系人。
点击联系人并将联系信息插入数据库。
这工作
我遇到的问题是它将页面上的所有while循环插入msql数据库,我需要一些帮助来解决这个问题。



在搜索激活时加载的PHP代码:



这是在一个while循环中:

 <?php 
echo'
< div id ='。$ get_memID。'class =box_content>
< table width =100%>
< tr>
< td width =50%align =left> '。$ profile_pic_lil。'& nbsp;< span style =font-size:14px; font-weight:bold; color:#060;>'。$ get_memfirstName。'& nbsp;'。$ get_memlastName 。'< /跨度>< / TD>
< td width =40%align =rightvalign =top>
< span style =font-size:12px; color:#999999;>'。$ linkUP_As。'< / span>
< input name =to_memtype =hiddenid =to_memvalue ='。$ get_mem。'/>
< input name =to_memIDid =to_memIDtype =hiddenvalue ='。$ get_memID。'/>
< input name =to_memfirstNameid =to_memfirstNametype =hiddenvalue ='。$ get_memfirstName。'/>
< input name =to_memlastNametype =hiddenvalue ='。$ get_memlastName。'/>
< / td>
< / tr>
< / table>
< / div>
';
?>

Jquery Script加载搜索:

<$ p $($。$)$($)$($)$($)$($)$($)$

clearTimeout(timer);

var ms = 200; //毫秒
var val = this.value;

timer = setTimeout(function(){
lookup(val);
},ms);

var dataString ='mysearchString ='+ val;

if(val ==''){
} else {
$ .ajax({
type:POST,
url:/happening_searchlinkUP.php,
data:dataString,
cache:false,
success:function(html){
$(#display<?php echo $ id;?>)。html(html ).show();
}
});
}

返回false;
});
});

jquery脚本将php文件发送给mysql:

  $(document).ready(function(){
$(。box_content)。live('click',function(e){
var element = $(this);
var id = element.attr(id);
var mem_email = $(#to_mem)。val();
var mem_firstName = $(#to_memfirstName)。val();
var happe_id = $(#happe_id<?php echo $ id;?>).val();
var info =id =+ id +'& mem_email ='+ mem_email +'& mem_firstName ='+ mem_firstName +'& happens_id ='+ happens_id;

$ .ajax({
type :POST,
url:/happening_getMem_linkUP.php,
data:info,
cache:false,
成功:function(html){
$ (#now_from_linkup<?php echo $ id;?>)。html(mem_firstName);
}
});

return false;
});
});

任何帮助都很乐意接受,并且提前致谢

解决方案

您将不得不取消您的ajax调用,最简单的方法是执行 doOnce

  var doOnce = 1; 
$(document).ready(function(){
$(。box_content)。live('click',function(e){
var element = $(this);
var id = element.attr(id);
var mem_email = $(#to_mem)。val();
var mem_firstName = $(#to_memfirstName)。 val();
var happe_id = $(#happe_id<?php echo $ id;?>)。val();
var info =id =+ id +'& mem_email ='+ mem_email +'& mem_firstName ='+ mem_firstName +'& happen_id ='+ happens_id;
if(doOnce){
doOnce--;
$ .ajax({
类型:POST,
url:/happening_getMem_linkUP.php,
data:info,
cache:false,
success:function(html){
$(#now_from_linkup<?php echo $ id;?>)。html(mem_firstName);
doOnce ++;
}
});
}
返回false;
});
});

doOnce 起始于true,所以ajax将被调用,并且 doOnce 将被禁用,禁用ajax调用,然后当ajax返回成功时, doOnce 将成为设置回true,允许它再次运行。



您也可以使用 Ben Alman的Throttle& Debounce插件


How do one only submit click event (div), when using jquery live function?

What i'm trying to do.

Click input and search for contact in mysql. Click on contact and insert contact info to database. This work The problem i'm have is that it insert all the while loop on page to msql database, i need some help fixing this.

PHP code that load when search activate:

This is in a while loop:

<?php 
echo '
<div id="'.$get_memID.'" class="box_content">
    <table width="100%">
        <tr>
            <td width="50%" align="left"> '.$profile_pic_lil.'&nbsp;<span style="font-size: 14px; font-weight: bold; color: #060; ">'.$get_memfirstName.'&nbsp;'.$get_memlastName.'</span></td>
            <td width="40%"align="right" valign="top">
                <span style="font-size: 12px; color: #999999;">'.$linkUP_As.'</span>
                <input name="to_mem" type="hidden" id="to_mem" value="'.$get_mem.'" />
                <input name="to_memID" id="to_memID" type="hidden" value="'.$get_memID.'" />
                <input name="to_memfirstName" id="to_memfirstName" type="hidden" value="'.$get_memfirstName.'" />
                <input name="to_memlastName" type="hidden" value="'.$get_memlastName.'" />   
            </td>
        </tr>
    </table>
</div>
'; 
?>

Jquery Script to load search:

$(document).ready(function(){
    var timer;                                         
    $(".search").live('keyup',function(e){

        clearTimeout(timer);

        var ms = 200; // milliseconds
        var val = this.value;

        timer = setTimeout(function() {
            lookup(val);
        }, ms);

        var dataString = 'mysearchString='+ val;

        if(val==''){
        } else {
            $.ajax({
                type: "POST",
                url: "/happening_searchlinkUP.php",
                data: dataString,
                cache: false,
                success: function(html){
                    $("#display<?php echo $id; ?>").html(html).show();
                }
            });
        }

        return false; 
    });
});

Jquery Script to send php file to mysql:

$(document).ready(function(){
    $(".box_content").live('click',function(e){                                    
        var element = $(this);
        var id = element.attr("id");
        var mem_email = $("#to_mem").val();
        var mem_firstName = $("#to_memfirstName").val();
        var happening_id = $("#happening_id<?php echo $id; ?>").val();
        var info = "id=" + id + '&mem_email=' + mem_email + '&mem_firstName=' + mem_firstName + '&happening_id=' + happening_id;            

        $.ajax({
            type: "POST",
            url: "/happening_getMem_linkUP.php",
            data: info,
            cache: false,
            success: function(html){
                $("#now_from_linkup<?php echo $id; ?>").html(mem_firstName);
            }
        });

        return false;
    });
}); 

Any help is gladly accepted, and thanks in advance

解决方案

You're going to have to debounce your ajax calls, the easiest way is to do a doOnce

var doOnce = 1;
$(document).ready(function(){
    $(".box_content").live('click',function(e){                                    
        var element = $(this);
        var id = element.attr("id");
        var mem_email = $("#to_mem").val();
        var mem_firstName = $("#to_memfirstName").val();
        var happening_id = $("#happening_id<?php echo $id; ?>").val();
        var info = "id=" + id + '&mem_email=' + mem_email + '&mem_firstName=' + mem_firstName + '&happening_id=' + happening_id;            
        if(doOnce){
        doOnce--;
        $.ajax({
            type: "POST",
            url: "/happening_getMem_linkUP.php",
            data: info,
            cache: false,
            success: function(html){
                $("#now_from_linkup<?php echo $id; ?>").html(mem_firstName);
                doOnce++;
            }
        });
        }
        return false;
    });
}); 

doOnce starts off true, so the ajax will be called and doOnce will be turned false disabling the ajax call, then when the ajax returns success the doOnce will be set back to true allowing it to run again.

You can also use Ben Alman's Throttle & Debounce plugin

这篇关于如何使用jquery live函数提交点击事件(div)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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