0

表单应该验证,如果它无效,表单不应该继续,但它会自动将应用程序转移到另一个页面......

HTML

<div data-role="page" id="newevent1">
<div data-role="header">
    <h1>New Event</h1>
</div>
<div data-role="content">
    <form id="newevent1info" name="newevent1info" align="left">
        <div data-role="fieldcontain">
            <label for="ename">Event Name:</label>
            <input align="right" type="text" id="ename" name="ename" />
        </div>
        <div data-role="fieldcontain">
            <fieldset data-role="controlgroup" data-type="horizontal">
                <legend>Date:</legend>
                <input type="date" id="date" name="date"/>
            </fieldset>
        </div>
        <div data-role="fieldcontain">
            <fieldset data-role="controlgroup" data-type="horizontal">
                <legend>Time:</legend>
                <input type="time" id="time" name="time" />
            </fieldset>
        </div>
        <div data-role="fieldcontain">
            <label for="descevent">Description: </label>
            <input type="text" id="descevent" name="descevent"/>
        </div>
        <button type="submit" name="submit">Create Event</button>
    </form>
</div>
</div>

JAVASCRIPT

    $('#newevent1info').submit(function (event) {
event.preventDefault();

if ($(this).validate({
rules: {
ename: {
required: true
},
descevent: {
required: true
},
date: {
required: true
},
time: {
required: true
},
}
}).form()) {

alert("success");

}
});

我希望它仅在所有表单数据都有效时才提醒成功。我需要解决什么问题?

4

1 回答 1

1

如果不为真,则简单地返回假。

$('#newevent1info').submit(function (event) {
    event.preventDefault();
    if ($(this).validate({
        rules: {
            ename: {
                required: true
            },
            descevent: {
                required: true
            },
            date: {
                required: true
            },
            time: {
                required: true
            },
        }
    }).form()){
            alert("success");
        } else {
            return false;
        }
});
于 2013-06-13T23:31:45.763 回答