简体   繁体   English

如何获得所需的自动对焦验证以在html表单上工作? -我的尝试无法验证

[英]How can I get my required autofocus validation to work on html form? - My attempt won't validate

Basically, I've tried to create a form which when completely filled and submit is clicked a new form is revealed in it's place. 基本上,我尝试创建一个表单,当完全填写并单击提交后,将显示一个新表单。

Although the complete and reveal works. 虽然完整而彰显作品。 The validation doesn't seem to, as I'm wanting the form to force the user to complete certain fields prior to advancing the user to the next form, 验证似乎没有,因为我想让表单强制用户在将用户推进到下一个表单之前先完成某些字段,

my code is below: 我的代码如下:

CSS: CSS:

  <style>
        .home-reservation-box {
            -moz-transition: opacity .25s ease-in-out;
            -webkit-transition: opacity .25s ease-in-out;
            transition: opacity .25s ease-in-out;
            visibility: hidden;
        }
  <style>

Javascript: Javascript:

<script>
    $(function() {
        $(".datepicker").datepicker({ minDate: 0 });
    });


    function change1() {
        document.getElementById("CalendarSelect").style.visibility = "hidden";
        document.getElementById("CalendarSelect2").style.visibility = "visible";
    }

    function change2() {
        document.getElementById("CalendarSelect2").style.visibility = "hidden";
        document.getElementById("CalendarSelect3").style.visibility = "visible";
    }


</script>

HTML: HTML:

    <form name="bookroom" >

        <input type="text" id="datefrom" name="book_date_from" placeholder="Please enter check in date" value="Check In" class="datepicker" readonly="true" required autofocus>
        <input type="text" id="dateto" name="book_date_to" placeholder="Please enter check out date" value="Check Out" class="datepicker" readonly="true" required autofocus>

        <div class="select-wrapper">
            <select id="adults" name="book_room_adults" placeholder="Please enter number of adults" required autofocus>

                <option value=" none">
                    Adults</option>
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>

        <div class="select-wrapper">
            <select id="children" name="book_room_children" placeholder="Please enter number of children" required autofocus>

                <option value="none">Children</option>
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>

        <input class="bookbutton" value=" check availability" onclick=" change1(); ">
    </form>


</div>

<div class="home-reservation-box" id="CalendarSelect2">

    <form name="bookroom">

        <input type="text" id="Name" name="book_date_from" value="Name" required autofocus>
        <input type="text" id="Surname" name="book_date_to" value="Surname" required autofocus>
        <input type="text" id="Phone" name="book_date_from" value="Phone">
        <input type="text" id="E-Mail" name="book_date_to" value="E-Mail" required autofocus>

        <input class="bookbutton" value="Check Availability" onclick=" change2(); ">

    </form>


</div>

<div class="home-reservation-box" id="CalendarSelect3">

    <p>Thanks for your request for a reservation. We're checking availability and we'll get back to you as soon as possible.</p>



</div>

When I add type submit to the input button and method="post", the validation still doesn't work and rather than allowing the user to enter information into the newly appeared form it simply disappears quickly 当我向输入按钮和method =“ post”添加类型Submit时,验证仍然不起作用,而不是允许用户将信息输入到新出现的表单中,而该表单很快就会消失

Validations are Kicked in when Submit button is clicked in HTML5. 在HTML5中单击“提交”按钮时,将启动验证。 You need to validate the code using your own script if you want to get the code validated. 如果要验证代码,则需要使用自己的脚本验证代码。 One thing you may try is to make a submit button and on clicking the submit check the validity and do the indented. 您可以尝试做的一件事是单击“提交”按钮,然后单击“提交”以检查有效性并缩进。 The Code may look like this. 该代码可能看起来像这样。 I have not tried it.. but you can try it. 我没有尝试过..但是你可以尝试。

$('[name="bookroom"]').submit(function(event){ if ($(this).checkValidity || $(this).checkValidity()) {
    document.getElementById("CalendarSelect").style.visibility = "hidden";
    document.getElementById("CalendarSelect2").style.visibility = "visible";
    event.preventDefault();

 }else { event.preventDefault(); } }); 

The problem is in this line: 问题在这一行:

<input class="bookbutton" value="Check Availability" onclick=" change2(); ">

While you meant for it to appear as Button actually, it is rendered out like a text box . 虽然您打算让它实际上显示为Button ,但它却像text box一样呈现出来。

jsFiddle jsFiddle

Here is what I did.. Since, you never want to submit anything yet, (only to trigger a scripted function), then you can use any element for that. 这就是我所做的。。既然您还不想提交任何东西(仅用于触发脚本功能),则可以使用任何元素。 I used <div> to act as a button. 我使用<div>充当按钮。

<div class='button' onclick="change1();">Check Availability!</div>

Then I added the CSS styling attributes for this div as follows: 然后,我为该div添加了CSS样式属性,如下所示:

div.button
{
    margin-top: 10px;
    border: 1px solid black;
    display: inline-block;
    padding: 10px;
   cursor: pointer;    
}

div.button:hover
{
    background-color: gold;
}

I also modified your code, since you already have jQuery, why use getElementById anymore? 我还修改了您的代码,因为您已经有了jQuery,为什么还要再使用getElementById These lines: 这些行:

function change1() {
        document.getElementById("CalendarSelect").style.visibility = "hidden";
        document.getElementById("CalendarSelect2").style.visibility = "visible";
    }

    function change2() {
        document.getElementById("CalendarSelect2").style.visibility = "hidden";
        document.getElementById("CalendarSelect3").style.visibility = "visible";
    }

became: 成为:

function change1() {
  alert('change1() is called');
  $('#CalendarSelect').hide();
  $('#CalendarSelect2').show();

}

function change2() {
  alert('change2() is called');
  $('#CalendarSelect2').hide();
  $('#CalendarSelect3').show();
}

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

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