简体   繁体   English

如果突出显示必填字段,如何显示隐藏的div?

[英]How to show an hidden div if a required field is highlighted?

for a series of circumstances I need to update an old ASP classic site. 对于一系列情况,我需要更新一个旧的ASP经典网站。 My users need to fill one form that is divided in two divs that they can activate (display) clicking on a button. 我的用户需要填写一个分为两个divs表格,他们可以单击按钮来激活(显示)。

Instead of doing complicate checks I found useful the required attribute of HTML 5 but unfortunately I have some problem when the required field is in the hidden part of the form. 我没有进行复杂的检查,而是发现HTML 5的required属性很有用,但是不幸的是,当required字段位于表单的隐藏部分时,我遇到了一些问题。

If the user submits the form and the required field (not filled) is located in the hidden div the browser displays the alert but, obviously, not the field (because is in the hidden div ). 如果用户提交表单,并且必填字段(未填写)位于隐藏的div则浏览器将显示警报,但显然不会显示警报(因为该字段位于隐藏的div )。

Is it possible to automatically show the hidden part of the form in which the required field is located? 是否可以自动显示表单中required字段所在的隐藏部分?

Some code on the fly: 一些即时代码:

<form action="anagraphic.asp" method="post">
    <div id="divName" style="display:inline">
        Name <input id="inputName" name="inputName" type="text" required /> 
        <input name="Button1" type="button" value="Next"   
            onclick="document.getElementById('divName').style.display='none';document.getElementById('divSurname').style.display='inline';"/>
    </div>
    <div id="divSurname"style="display:none">
        <input name="Button2" type="button" value="Previous" 
            onclick="document.getElementById('divSurname').style.display='none';document.getElementById('divName').style.display='inline';" />
        Surname <input  id="inputSurname" name="inputSurname" type="text" required  />
        <input name="Submit1" type="submit" value="submit" />
    </div>

</form>

Let's say that you "forget" to fill the name field and you click on Next . 假设您“忘记”填写姓名字段,然后单击“ 下一步” You fill surname and you submit the form: immediately the embedded alert appears. 您填写姓氏并提交表格:嵌入的警报立即出现。 Substantially I would like to intercept the event that highlights the required field and at the same time show the first div the one with the name. 基本上,我想截取突出显示必填字段的事件,同时向第一个div显示一个名称相同的事件。

Is it possible? 可能吗?

You could use jQuery when the submit button is clicked to show any required fields that are not visible: 单击提交按钮时,可以使用jQuery来显示所有不可见的必填字段:

function foo()
{
  $("input[required]:hidden").parent().show()
}

And let the submit button call the function when clicked: 然后单击“提交”按钮调用该函数:

<input name="Submit1" type="submit" value="submit" onclick=foo()/>

A much simpler logic will be if you check the name field onclick of Button1. 如果您检查Button1的名称字段onclick,则逻辑会简单得多。 If the name field is valid you can hide that and show the surname field. 如果名称字段有效,则可以将其隐藏并显示姓氏字段。 This will avoid showing already hidden name field, which breaks the ui flow and may be confusing for the user. 这将避免显示已经隐藏的名称字段,这会破坏ui流程,并可能使用户感到困惑。

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

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