简体   繁体   English

如何使用Javascript / jQuery隐藏/显示启用/禁用HTML元素?

[英]How hide/show enable/disable HTML elements using Javascript/jQuery?

Here is what I have : 这是我所拥有的:

IF the user does't update their email, hide the email_again 如果用户未更新电子邮件,请再次隐藏email_

Email _______________ 电子邮件_______________

ELSE show email_again ( email has been up dated ) ELSE显示email_again(电子邮件已更新)

Email _______________ 电子邮件_______________

Email Again _______________ 重发电邮 _______________

GOAL 目标

IF there is an input on email_again, and it's fail the validation when submitting, I want to 如果is一个input上email_again,它的fail在提交的时候验证,我想

⁃   redirect with old input
⁃   show the label of email_again
⁃   show the input of email_again
⁃   enable email_again

ELSE 其他

⁃   Redirect with old input
⁃   Give them error message that Email Again is required
⁃   show the label of email_again
⁃   show the input of email_again
⁃   enable email_again

CODE

  var old_email = $("#email").val();
  $('#email').on('input', function (event) {

  var email = $(this).val();

  if (email === old_email) { 

    $('#email_again').prop('disabled', true);
    $('#email_again_label').hide();
    $('#email_again').hide();

  } else {

    $('#email_again').prop('disabled', false);
    $('#email_again_label').show();
    $('#email_again').show();

  }

}); 

</script>

Can someone help me tweak my code ? 有人可以帮我调整我的代码吗? or suggest any better choices. 或提出任何更好的选择。

Please try if this is what you need: DEMO 如果需要,请尝试: DEMO

HTML: HTML:

<label for="email" id="labemail">E-Mail</label>
<input id="email" name="email" type="text"/>
<label for="emailagain" id="labemailagain" style="display:none">E-Mail again</label>
<input id="emailagain" type="text" name="emailagain" style="display:none"/>
<button id="click">Click</button>

Javascript: Javascript:

var a = document.getElementById('email');
var al = document.getElementById('labemail');
var bl = document.getElementById('labemailagain');
var b = document.getElementById('emailagain');
var c = document.getElementById('click');
a.addEventListener('keyup',function() {
bl.style.display = '';
b.style.display = '';
});
c.addEventListener('click',function(){

    if (a.value == b.value) {
        al.style.color = 'green';
        bl.style.color = 'green';
        alert('OK - update');
    }
    else if (a.value != '' && b.value == '')
    {
        bl.style.color = 'red';
        alert('You need to repeat your email');
    }
    else if (a.value != b.value)
    {
        bl.style.color = 'red';
        al.style.color = 'red';
        alert('E-Mails does not match');
        a.value = a.value;
        b.value = '';

    }


});

I have worked on this, Check this out. 我已经为此工作了,请查看此。 Also check out the DEMO link. 还要检查DEMO链接。

HTML Part: HTML部分:

<div class="container">
    <div class="inputContainer" id="email_container">
        <label for="email">E-Mail</label>
        <input id="email" name="email" type="text"/>
        <span class="inputError" id="email_error"></span>
    </div>
    <div class="inputContainer displayNone" id="email_re_container">
        <label for="email_re">E-Mail Again</label>
        <input id="email_re" name="email_re" type="text"/>
        <span class="inputError" id="email_re_error"></span>
    </div>
    <div class="inputContainer" id="submit_container">
        <button id="submit_email">Submit</button>
    </div>
</div>

CSS Part: CSS部分:

.container {
    float: left;
    width: 180px;
}
.inputContainer {
    float: left;
    min-height: 50px;
}
.displayBlock {
    display: block;
}
.displayNone {
    display: none;   
}
.inputError {
     text-align: right;
    color: #FC0C0C;
    display: block;
    width: 95%;
    font-weight: bold;
    min-height: 15px;
}

JS Part: JS部分:

$(document).ready(function(){
    $("#email").keyup(function(){
        var email_value = $("#email").val();
        if(email_value != null && email_value != undefined && email_value.trim() != "") {
            $("#email_re_container").removeClass("displayNone");
            $("#email_re_container").addClass("displayBlock");
        }
        else {
            $("#email_re_container").removeClass("displayBlock");
            $("#email_re_container").addClass("displayNone");
            $("#email_re").val("");
        }
    });
    $("#submit_email").click(function(){
        var email_value = $("#email").val();
        var email_re_value = $("#email_re").val();
        if(email_value == null || email_value == undefined || email_value.trim() == "") {
            $("#email_re_container").removeClass("displayBlock");
            $("#email_re_container").addClass("displayNone");
            $("#email_re").val("");
            $("#email").val("");
            alert("Email is Required");
        }
        else if(email_re_value == null || email_re_value == undefined || email_re_value.trim() == "") {
            $("#email_re").val("");
            alert("Email Again is Required");
        }
        else if(email_value != email_re_value) {
            $("#email_re").val("");
            alert("Value Doesn't Matches");
        }

        else if(email_value == email_re_value) {
            alert("Email Matches");
        }
        else {
            alert("Error Occurred");
        }
    });
});

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

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