简体   繁体   English

jQuery表单验证不起作用

[英]Jquery Form Validation does not work

I have this JQuery Mobile Form, which sends the inputs to a MySQL Database. 我有这个JQuery移动表单,它将输入发送到MySQL数据库。 I would like to validate all fields as required. 我想根据需要验证所有字段。 However, it does not work and I cannot find the error. 但是,它不起作用,我找不到错误。

Here is the first part of the form. 这是表格的第一部分。 I put "required" on the input fields. 我在输入字段上输入了“ required”。

<form id="form-haftpflicht" method="post">
<div data-role="fieldcontain">
    <fieldset data-role="fieldcontain">
        <label for="versicherungsbeginn"><b>Versicherungsbeginn</b></label>
        <input type="date" name="versicherungsbeginn" id="versicherungsbeginn" value="" required />
    </fieldset>
    <fieldset data-role="fieldcontain">
        <label for="erwachsene" class="select"><b>Anzahl Erwachsene:</b></label>
        <select name="erwachsene" id="erwachsene" data-role="none" required>
            <option value="standard" data-placeholder="true">-- Bitte wählen --</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>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>

        </select>
    </fieldset>
    <fieldset data-role="fieldcontain">
        <label for="kinder" class="select"><b>Anzahl Kinder:</b></label>
        <select name="kinder" id="kinder" data-role="none" required>
            <option value="standard" data-placeholder="true">-- Bitte wählen --</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>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>

        </select>
    </fieldset>
</div>

And here is my script with the API 这是我的API脚本

 $(document).ready(function () {

        $("#store-haftpflicht").click(function () {
            $("#form-haftpflicht").submit();
        });
        $("#form-haftpflicht").submit(function (event) {
            event.preventDefault();
            $("#ajax-loader").css("display", "block");
            $.ajax({
                url: 'http://app.lovanet.ch/app/store_haftpflicht.php',
                data: $(this).serialize(),
                method: 'POST',
                success: function (data, status) {
                    $("#ajax-loader").css("display", "none");
                },
                error: function () {
                    output.text('Keine Prämien gefunden.');
                }
            });
        });
    });

A select is considered empty (and then invalid if the field is required) if the selected option has an empty value (""), but yours have a value ("standard"). 如果所选选项的值为空(“”),而您的选项为“标准”,则select被认为是空的(如果需要该字段,则无效)。

To fix the problem with validation, just replace the "standard" value in the first option for a "": 要通过验证解决问题,只需将第一个选项中的“标准”值替换为“”即可:

<select name="kinder" id="kinder" data-role="none" required>
    <option value="" data-placeholder="true">-- Bitte wählen --</option>
    <option value="1">1</option>
    <option value="2">2</option>

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

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