简体   繁体   English

JavaScript单击一次

[英]Javascript click once

I have the following javascript in my website: 我的网站上有以下javascript:

$('#example tbody').on( 'click', 'input', function () { 
            var data = table.row( $(this).parents('tr') ).data();

            $(".iframe").colorbox({href:"session_edit.php?ID="+data[0]});

            $(".iframe3").colorbox({href:"delete.php?ID="+data[0]});

            $(".iframe2").click(function() {location.href = "record_dt.php?ID="+data[0]});  


        });


    } );

When clicking the respective buttons on my datable 'iframe' and 'iframe3' work fine with a normal single click. 单击我的datable“ iframe”和“ iframe3”上的相应按钮时,只需单击一次即可正常工作。 However, when i click on the respective button for iframe2 I have to click twice for the button to respond. 但是,当我单击iframe2的相应按钮时,我必须单击两次以使该按钮响应。 Not necessarily double click but one click and then another. 不一定双击,而是单击然后再单击。 Any idea why this is happening? 知道为什么会这样吗? Since 'iframe' and 'iframe3' are associated with colorbox this is the respective code: 由于'iframe'和'iframe3'与colorbox相关联,因此这是各自的代码:

FULL CODE: 完整代码:

<script>

$(document).ready(function()
        {
            $(".iframe").colorbox({iframe:true, width:"700px", height:"80%"});
            $(".iframe3").colorbox({iframe:true, width:"300px", height:"20%", onLoad: function() {
                $('#cboxClose').remove();
            }});


        });

</script>

<script type="text/javascript" language="javascript" class="init">
    $(document).ready(function() {
        var table = $('#example').DataTable( {
            "columnDefs": [ {
                "targets": -1,
                "data": null,
                "defaultContent": "<input type='image' src='delete.png' id='button' >"
            },

            {
                "targets": -2,
                "data": null,
                "defaultContent": "<input type='image' src='edit.png' id='button' >"
            },

            {
                "targets": -3,
                "data": null,
                "defaultContent": "<input type='image' src='edit.png' id='button'>"
            }

             ],
            "order": [[ 0, "desc" ]]
        } );


        var data = false;
        $('#example tbody').on('click', 'input', function(){ 
            // on input click, set the data to new value
            data = table.row( $(this).parents('tr') ).data();
            $(".iframe").colorbox({href:"session_edit.php?ID="+data[0]});
            $(".iframe3").colorbox({href:"delete.php?ID="+data[0]});
        });
        $(".iframe2").click(function()
                {

            if(data) {location.href = "record_dt.php?ID="+data[0];}

            }); 


    });


    </script>

These are working fine with a single click. 一次单击即可正常工作。 The problem is 'iframe2'. 问题是“ iframe2”。

Simply move your click event trigger to outside the other event trigger for your tbody: 只需将您的点击事件触发器移动到您的肢体的其他事件触发器之外

// Since the table2 click event needs to know about the data value as well,
// set it as a global, shared variable.
var data = false;
$('#example tbody').on('click', 'input', function(){ 
    // on input click, set the data to new value
    data = table.row( $(this).parents('tr') ).data();
    $(".iframe").colorbox({href:"session_edit.php?ID="+data[0]});
    $(".iframe3").colorbox({href:"delete.php?ID="+data[0]});
});
$(".iframe2").click(function(){
    // check if data is not false (aka unset), if not, execute location change
    if(data) location.href = "record_dt.php?ID="+data[0]}); 
});

Now the click event is attached on load and not after the initial click on tbody , which resulted in your initial need to click twice. 现在,单击事件附加在加载时,而不是最初单击tbody ,因此您最初需要单击两次。

You are defining iframe2 click event inside $('#example tbody') click event. 您正在$('#example tbody')click事件中定义iframe2 click事件。 So on first click it binds click event and then second time it works. 因此,在第一次单击时,它将绑定click事件,然后在第二次运行时。

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

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