【问题标题】:automatic validation does not appear when adding functionality to submit添加功能以提交时不会出现自动验证
【发布时间】:2014-04-21 09:55:41
【问题描述】:

我有这样的脚本,(使用 jquery.validate.js)

input.php

<?php
if ($input=='inputform'){
echo $_POST[username].'-'.$_POST[email];
}
?>

form.php

<script type="text/javascript">
$(document).ready(function() {
var validator = $("#signupform").validate({
    rules: {
        username: {
            required: true,
            minlength: 2,
            remote: "users.action"
        },
        email: {
            required: true,
            email: true,
            remote: "emails.action"
        }
    },
    messages: {
        username: {
            required: "enter your name",
            minlength: jQuery.format("enter at least {0} character"),
            remote: jQuery.format("{0} is already in use")
        },
        email: {
            required: "enter your email",
            minlength: "enter your email",
            remote: jQuery.format("{0} is already in use")
        }
    }
}); 

//automatic validation does not appear when users type in error. It happens when I make a submit function like this. 
$("#sign").CheckAndSubmit(function()
    {         
        window.onload {
        document.getElementById('signupform').submit();
    }
    }); 

 });
</script>

<form id="signupform" action="input.php?input=inputform" method="post" onsubmit="return CheckAndSubmit()">
        <table>
    <tr>
        <td class="label">Name</td>
            <td><input id="username" name="username" type="text" value="" maxlength="50" /></td>
            <td class="status"></td>
    </tr>
    <tr>
        <td class="label">Email</label></td>
            <td><input id="email" name="email" type="text" value="" maxlength="150" /></td>
            <td class="status"></td>
    </tr>
    </table><table>     
    <tr>
        <td></td>
        <td><input name="submit" type="submit" value="Order"> </td>
    </tr>
</table>

我希望出现自动验证并运行提交过程。在本节中是否有人可以帮助解决这个问题。之前谢谢你

$("#sign").CheckAndSubmit(function()
    {         
        window.onload {
        document.getElementById('signupform').submit();
    }
    }); 

【问题讨论】:

    标签: jquery validation window submit onload


    【解决方案1】:

    因为使用脚本调用提交不会触发点击事件。所以试试

    $('#signupform').submit();
    

    而不是

    document.getElementById('signupform').submit();
    

    演示:Fiddle

    注意:您的脚本中还有其他语法错误,名称为 submit 的提交按钮将覆盖表单方法 submit,这将阻止 form.submit() 调用,因此请重命名按钮。

    【讨论】:

    • 是的,先生,我的脚本确实没有错误,但是如何自动验证并提交工作,因为它应该可以正常工作。因为当我给提交功能时,自动验证不起作用
    • 你有没有通过替换提交功能来尝试我的答案
    • 是的,我试过了,他如何在 input.php 上按预期发布结果?
    • @ivanichi 在那种情况下$("#sign").click(function () { if ($('#signupform').valid()) { $('#signupform')[0].submit(); } }); - jsfiddle.net/arunpjohny/4mAdr/5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多