JavaScript 点击一次

标签 javascript jquery html colorbox

我的网站中有以下 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]});  


        });


    } );

当单击我的数据表“iframe”和“iframe3”上的相应按钮时,只需正常单击即可正常工作。但是,当我单击 iframe2 的相应按钮时,我必须单击两次按钮才能响应。不一定是双击,而是单击一下,然后再单击一次。知道为什么会发生这种情况吗?由于“iframe”和“iframe3”与 colorbox 相关联,因此这是各自的代码:

完整代码:

<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>

只需单击一下即可正常工作。问题是“iframe2”。

最佳答案

只需将您的点击事件触发器移至您的 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]}); 
});

现在,点击事件是在加载时附加的,而不是在首次点击 tbody 后附加的,这导致您最初需要点击两次。

关于JavaScript 点击一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693705/

相关文章:

javascript - 多部分/表单数据上传 - Nodejs - expressjs

javascript - 用于调用方法的条件运算符

javascript - 如何添加适用于两个下拉菜单的 "random"按钮

javascript - 需要修复 Chrome 和 IE 中的 Tooltip Position

javascript - 当类成员仍然存在于页面状态时如何编写

javascript - 使用 jquery 操作复选框和文本框

jquery单击空白部分时更改td的背景颜色

javascript - 将用户重定向到打开模式的页面

html - 中间对齐具有不同字体大小的右浮动跨度的表格单元格内容

javascript - 在表单中动态添加输入(无法在 PHP 中获取值)