检查空输入,然后运行脚本

时间:2014-07-30 18:42:36

标签: javascript jquery html

有没有办法运行一个脚本来检查#username-input是否为 document.getElementById('login-button').addEventListener('click', function () { var name = document.getElementById('username-input').value; document.getElementById('username').innerHTML = 'Welcome ' + name; document.getElementById('username-input').remove(); document.getElementById('password-input').remove(); document.getElementById('login-button').remove(); }, false); 空,然后运行此脚本:

#username-input

修改

如果#password-inputalert('username or password can't be blank') 为空 弹出一个警告,说不是运行脚本

{{1}}

3 个答案:

答案 0 :(得分:0)

只需使用if语句检查div是否为空。

if($('#username-input').text() != '') {
    document.getElementById('login-button').addEventListener('click', function ()
        {
            var name = document.getElementById('username-input').value;
            document.getElementById('username').innerHTML = 'Welcome ' + name;
            document.getElementById('username-input').remove();
            document.getElementById('password-input').remove();
            document.getElementById('login-button').remove();
        }, false);
}

用于用户名和密码检查。

if($('#username-input').text() != '' || $('#password-input').text() != '') {
    alert('username or password can't be blank')
}

答案 1 :(得分:0)

似乎你没有使用JQuery所以这样就可以了。

if (document.getElementById('username-input').value != "") {
    document.getElementById('login-button').addEventListener('click', function () {
        var name = document.getElementById('username-input').value;
        document.getElementById('username').innerHTML = 'Welcome ' + name;
        document.getElementById('username-input').remove();
        document.getElementById('password-input').remove();
        document.getElementById('login-button').remove();
    }, false);
}

答案 2 :(得分:0)

    //define the on success function.
    var onSuccess = function () {
        document.getElementById('login-button').addEventListener('click', function ()
        {
            var name = document.getElementById('username-input').value;
            document.getElementById('username').innerHTML = 'Welcome ' + name;
            document.getElementById('username-input').remove();
            document.getElementById('password-input').remove();
            document.getElementById('login-button').remove();
        }, false);
    }
    //Get the username and password.
    var userName = document.getElementById('username-input').value;
    var userName = document.getElementById('password-input').value;

   //validate the input
   //This if statement will check if either userName or password is null, 0, "", or undefined.
    if (userName && password) {
        onSuccess();
    }
    else {
        alert("username or password can't be blank");
    }