简体   繁体   English

JQuery .click()仅在第二次单击后触发

[英]JQuery .click() triggers after the second click only

I'm trying to use JQuery load() instead of javascript ajax. 我正在尝试使用JQuery load()代替javascript ajax。 I have multiple of ids generated for each DIV with php loop. 我有多个php循环为每个DIV生成的ids

When creating a JQuery function like below I pass the generated ids of divs fine but, I will have to click twice on the button in-order the .click() event to trigger , I put a window.alert() inside the .click() function and it popups twice after the second click on the button, any idea how to do it the correct way? 当创建一个jQuery函数像下面我通过申报单的生成的ID不错,但我将不得不在按钮上点击两次序.click()事件来触发,我把window.alert()里面.click()函数,第二次单击该按钮后,它会弹出两次,您知道如何正确执行此操作吗?

UPDATED 更新

function updateDivs(generatedID){
    $('#'+generatedID).click(function(){
        //do some .load() stuff
    });
}

<?php
 for($i=0 ; $i < 20 ; $i++){
   $gen = mt_rand(100,999);
?>
   <div id="<?=$gen;?>" onclick="updateDivs(<?=$gen;?>) > some content </div
<?php
}
?>

I think what you want is binding a doubleclick event handler, which can be done this way: 我认为您想要的是绑定doubleclick事件处理程序,可以通过以下方式完成:

$("<?=$gen;?>").on("dblclick", function (e) {
   // do some stuff here.
});

I think that the easiest way is to set a class for your divs and inside you can get the div id with event.target.id. 我认为最简单的方法是为div设置一个类,在内部可以通过event.target.id获取div ID。

I think that your problem is that you need to have something like document.ready() or $(function() { }); 我认为您的问题是您需要使用document.ready()或$(function(){})之类的东西;

<div id="div_id" class="mydiv"> some content </div>
<script type="text/javascript">
           $(document).ready(function () {
        $(".mydiv").click(function (event) {
            alert(event.target.id);
        });
    });
</script>

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

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