简体   繁体   English

HTML5本地存储,验证所需的文本框问题

[英]HTML5 local storage, validating required text box issue

I have a survey. 我有一项调查。 On button click need it to validate certain fields required/store locally/ go to confirmation page(to prevent user from resubmitting). 在按钮上单击,需要它来验证某些必填字段/在本地存储/转到确认页面(以防止用户重新提交)。 Only 'FirstName''Hostipal' required atm. 仅'FirstName'Hostipal'需要atm。

Problem is when all required fields are filled, it fails to go to confirmation.html. 问题是,当所有必填字段均已填写时,它无法转到confirmation.html。 If i leave 1 required field open, It doesnt validate and goes to confirmation. 如果我将1个必填字段保留为开放状态,则它不会验证,而是进入确认状态。 If all 'required' syntax is taken out, It doesnt go to confirmation 如果所有“必需”语法都被删除,则不会进行确认

<label>First Name*:</label> <input required title="Name is required" id="FirstName" name="MainName" type="text"/>

In all cases, It still stores to local storage however. 在所有情况下,它仍然存储到本地存储中。 Any input on validating required fields would be appreciated. 任何有关验证必填字段的输入将不胜感激。 Hopefully can put it in my clicked() function. 希望可以将其放在我的clicked()函数中。

<button type="submit" value="Save" id="Save" >Submit Form</button>  

function 功能

$( document ).ready(function() {
    $('#Save').click(function (e) {
        if (confirm('Are you sure you want to submit? You will not be able to go back.')) {
            var person = $("#FirstName").val() + "." + $('#LastName').val();
            $('input, select, textarea').each(function () {
                var value = $(this).val(),
                    name = $(this).attr('name');
                localStorage[person + "." + name] = value;
                window.location.href = "Confirmation.html";
                console.log('stored key: ' + name + ' stored value: ' + value);
            });

        }
    });
});

If the above doesn't show my problem, here is the whole: http://jsfiddle.net/smZHe/1/ 如果以上内容没有显示我的问题,请执行以下操作: http : //jsfiddle.net/smZHe/1/

each helper method in jquery executes the function we pass to it once for each item in the initial array. jQuery中的每个辅助方法都会对初始数组中的每个项目执行一次传递给我们的函数。 You are trying to execute below statement multiple times (once for each input, select, textarea). 您试图多次执行以下语句(每次输入一次,选择一次,选择textarea)。

window.location.href = "Confirmation.html";

You can use a variable instead as flag to mark validation failure. 您可以使用变量代替标记来标记验证失败。 In the end, you can check variable and navigate conditionally. 最后,您可以检查变量并有条件地导航。

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

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