Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在鼠标上,如果选中复选框,则执行X,否则执行Y_Javascript_Jquery_Html_Css_Checkbox - Fatal编程技术网

Javascript 在鼠标上,如果选中复选框,则执行X,否则执行Y

Javascript 在鼠标上,如果选中复选框,则执行X,否则执行Y,javascript,jquery,html,css,checkbox,Javascript,Jquery,Html,Css,Checkbox,我试图在页面上设置一个复选框,以便在密码框中显示/隐藏密码。若它们是隐藏的,那个么它们将显示在该项目的悬停处 我唯一要解决的问题是,当jquery的其余部分加载时,var保存在页面加载中。如何在每次鼠标输入/离开时确保复选框处于选中状态 使用.is(“:checked”)检查复选框是否选中 window.hide_passwords = $('#hide_passwords').attr('checked'); $("main table tbody tr").on({

我试图在页面上设置一个复选框,以便在密码框中显示/隐藏密码。若它们是隐藏的,那个么它们将显示在该项目的悬停处

我唯一要解决的问题是,当jquery的其余部分加载时,var保存在页面加载中。如何在每次鼠标输入/离开时确保复选框处于选中状态

使用
.is(“:checked”)
检查复选框是否选中

window.hide_passwords = $('#hide_passwords').attr('checked');


    $("main table tbody tr").on({
        mouseenter: function () {
            if ($("#hide_passwords").is(":checked")) {
                $(this).children('td:first-child').children('input').attr('type', 'text');
            }
        },
        mouseleave: function () {
            if ($("#hide_passwords").is(":checked")) {
                $(this).children('td:first-child').children('input').attr('type', 'password');
            }
        }
    });
var window.hide_passwords=document.getElementById(“hide_passwords”);
window.hide_passwords=window.hide_passwords.checked
var container=document.queryselectoral(“主表tbody tr”);
变量i=0,长度=container.length;
对于(i;i

我只在原生JS中工作,很抱歉断开连接。

您可以对相同的行为使用绑定和取消绑定方法

window.hide_passwords = $('#hide_passwords').attr('checked');
showPass();

function showPass() {
    $("main table tbody tr").on({
        mouseenter: function () {
            $(this).children('td:first-child').children('input').attr('type', 'text');
        },
        mouseleave: function () {
            $(this).children('td:first-child').children('input').attr('type', 'password');
        }
    });
}

$('#hide_passwords').click(function () {
    if ($(this).prop('checked')) {
        showPass();
    } else {
        $('input[type="password"]').attr('type', 'password');
        $("main table tbody tr").unbind();
    }
});

可能的重复我说的是错误的,因为我说这行不通。我创建了一个演示来测试将输入类型从密码更改为文本的不同可能性,反之亦然:啊。
是你这个卑鄙的家伙,这就是为什么我尝试时它不起作用的原因:选中
var window.hide_passwords = document.getElementById("hide_passwords");
window.hide_passwords = window.hide_passwords.checked
var container = document.querySelectorAll("main table tbody tr");
var i = 0, length = container.length;
for (i; i < length; i++) {
    container[i].addEventListener("mouseover", function() {
        if (window.hide_passwords) {
            // Your stuff here
        } else {

        }
    }
}
window.hide_passwords = $('#hide_passwords').attr('checked');
showPass();

function showPass() {
    $("main table tbody tr").on({
        mouseenter: function () {
            $(this).children('td:first-child').children('input').attr('type', 'text');
        },
        mouseleave: function () {
            $(this).children('td:first-child').children('input').attr('type', 'password');
        }
    });
}

$('#hide_passwords').click(function () {
    if ($(this).prop('checked')) {
        showPass();
    } else {
        $('input[type="password"]').attr('type', 'password');
        $("main table tbody tr").unbind();
    }
});