如何添加 Validate() 函数



我有这个表格:

<form method='post' id='registration'>
...
<input type='submit' value='Submit' />
</form>

以及通过 POST 发送表单的脚本:

<script>
    $(document).ready(function(){
    $( "#registration" ).submit(function(event) {
        // Stop form from submitting normally
        event.preventDefault();
        // Get some values from elements on the page:
        var form = $(this);
        var username = form.find( "input[name='username']" ).val();
        var password = form.find( "input[name='password']" ).val();
        var url = "action.php";
        // Send the data using post
        $.post( url, {
              username: username,
              password: password
        });
        });
    });
</script>

我也写了 Validate() 函数。在哪里添加它以及如何添加它?

我个人会在帖子之前这样做。

if (validate()) {
    $.post();
{

这要求你的函数 validate() 在表单有效的情况下返回 false 或 true。

@Rebirth说

得很对。

@Nikolay Tsonev,你为什么不把

    var form = $(this);
    var username = form.find( "input[name='username']" ).val();
    var password = form.find( "input[name='password']" ).val();
    var url = "action.php";

到你的 Validate() 函数中。它只会直接喜欢;

event.preventDefault();
if (validate()) {
    $.post();
{

最新更新