简体   繁体   English

表单验证成功时显示隐藏的元素

[英]Show hidden element when form validation is successful

I'm trying to show a div if all fields of a HTML form are filled with at least one character. 我试图显示一个div如果HTML表单的所有字段都至少填充了一个字符。

<input name="name" id="name" value="" required placeholder="Name"></input>
<input name="surname" id="surname" value="" required placeholder="Surname"></input>
<input name="email" id="email" value="" required placeholder="Email"></input>
<textarea name="comments" value="" required placeholder="Comments"></textarea>
<div id="test" style="display:none;">test</div>

and I've got this script 我有这个剧本

<script type="text/javascript">
    if(document.getElementById('name').value!='' && 
       document.getElementById('surname').value!='' && 
       document.getElementById('email').value!='' && 
       document.getElementById('message').value!=''){
           document.getElementById('test').style.display = 'block';
    }
</script>

but it doesn't work. 但这不起作用。 Why? 为什么? I tried to move the script from top to the bottom of the file but the div 'test' is always hidden. 我试图将脚本从文件的顶部移至底部,但div'test'始终处于隐藏状态。 what is wrong with my sciript? 我的抄本怎么了?

You need to call that script every time a field is changed. 您需要在每次更改字段时调用该脚本。

For instance: 例如:

<input name="name" id="name" value="" required placeholder="Name" onchange="myFunction()"></input>
etc.

<script type="text/javascript">
    function myFunction() {
        if(document.getElementById('name').value!='' &&
           document.getElementById('surname').value!='' &&
           document.getElementById('email').value!='' &&
           document.getElementById('message').value!=''){
               document.getElementById('test').style.display = 'block';
        }
    }
</script>

I see you linked jquery. 我看到你链接了jQuery。 Why not use it if you have it? 为什么不使用它呢?

$('input').on('change', function(){
    if($('#name').val() != '' && $('#surname').val() != '' && $('#email').val() != '' && $('#comments').val() != ''){
        $('#test').show();
    }else{
        //This part is optional
        $('#test').hide();
    }
});

You should also add id="comments" in your textarea 您还应该在文本区域中添加id="comments"

You have two errors, first you have not assigned any ID to your textarea which you are using in your script. 您有两个错误,首先您没有为脚本中使用的textarea分配任何ID。 Second, the function must be called upon everytime the user makes any change, so you need to bind the onchange event. 其次,每次用户进行任何更改时都必须调用该函数,因此您需要绑定onchange事件。

So, it should be: 因此,应为:

HTML: HTML:

<input name="name" id="name" value="" required placeholder="Name" onchange="myUpdateFunction()"></input>
<input name="surname" id="surname" value="" required placeholder="Surname" onchange="myUpdateFunction()"></input>
<input name="email" id="email" value="" required placeholder="Email" onchange="myUpdateFunction()"></input>
<textarea name="comments" id="message" value="" required placeholder="Comments" onchange="myUpdateFunction()"></textarea>
<div id="test" style="display:none;">test</div>

JavaScript: JavaScript:

<script type="text/javascript">
function myUpdateFunction() {
    if(document.getElementById('name').value!='' &&
       document.getElementById('surname').value!='' &&
       document.getElementById('email').value!='' &&
       document.getElementById('message').value!='') {
           document.getElementById('test').style.display = 'block';
    }
}
</script>

The first issue is when the script is running. 第一个问题是脚本何时运行。 As coded it will run as soon as the page renders the script, and never again. 按照编码,它将在页面呈现脚本后立即运行,并且不再运行。

You will probably want to wire it up to the on change event of each text box so you can know when to show the hidden field. 您可能希望将其连接到每个文本框的on change事件,以便您知道何时显示隐藏字段。

Second, where is the "message" element? 第二,“消息”元素在哪里? Is that supposed to be comments? 那应该是评论吗? If so, comments is missing an Id (has a name but no Id) 如果是这样,则注释缺少一个ID(具有名称但没有ID)

http://jsfiddle.net/7hafkwhj/1/ http://jsfiddle.net/7hafkwhj/1/

First of all it could be a good idea to wrap your elements inside an html tag form: 首先,将元素包装在html标签形式中可能是一个好主意:

<form id="form">
    <input name="name" type="text" placeholder="Name" required></input>
    <input name="surname" type="text" placeholder="Surname" required></input>
    <input name="email" type="email" placeholder="Email" required></input>
    <textarea name="comment" placeholder="Comments" required></textarea>
</form>
<div id="message">Ok, roger</div>

Also, I don't understand why you should use jQuery for this kind of stuff. 另外,我不明白为什么您应该使用jQuery来处理此类问题。 You can make the same thing using native Javascript: 您可以使用本机Javascript做同样的事情:

(function() {
    var form = document.getElementById('form'),
    message = document.getElementById('message');

    function makeCheck() 
    {
        i = 0;
        while(i < form.elements.length) {
            if(form.elements[i].value === '')
            {
                return false;
            }
            i++;
        }
        return true;
    }

    function displayMessage() 
    {
        if(makeCheck()) 
        {
            message.style.display = 'block';
        }
        else 
        {
            message.style.display = 'hide';
        }
    }

    for(i = 0; i < form.elements.length; i++)
    {
        form.elements[i].onkeyup = function () {
            displayMessage();
        };
    }
})();

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

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