形成不提交内部$就成功功能功能

2023-09-10 14:19:33 作者:甜你许久

我通过使用jQuery + Ajax的验证重复的名称。一切正常,只是形式不提交一旦一切都返回true

I'm validating for duplicate names by using jquery+Ajax. Everything is working fine except that the form is not submitting once everything returns true

这是怎么回事

如果没有输入名称,警示框显示出来,说明名字 要求 - >没有问题就在这里 如果重名被发现,警告框显示出来,说明名 已经存在并形成不提交 - >没有问题就在这里 如果没有发现重复的名字,警告框显示出来(证明 其他 的条件的部分工作),但在形式不 提交。我想要的形式继续前进,并提交自己在这个 其他 部分 If no name is entered, alert box is showing up stating name is required --> No problem here If duplicate name is found, alert box is showing up stating name already exists and form does not submit --> No problem here If duplicate name is not found, alert box is showing up (to prove the else part of the condition is working), but the form does not submit. I want the form to go ahead and submit itself in this else part

jQuery的code

$('#form1').submit(function(){

    var name = $('#shelf_name').val();

    if(name == '')
    {
        alert('Shelf name is required');
        $('#shelf_name').focus();
    }
    else
    {                   
        $.ajax({
            type:'post',
            url:'check-duplicate-shelf-name.php',
            data:{'name':name},
            context:this,
            success:function(data)
            {
                if(data == 'stop')
                {
                    alert('Shelf name already exists'); // working if duplicate name is found
                }
                else
                {   
                    alert('else working?'); // alert box is showing up if name is not duplicate                                         
                    this.submit(); // but after alert, this line not executing
                }
            }
        });
    }


    return false;

});

HTML表单标签

<form action="add-shelf-post.php" method="post" id="form1">

检查重复的,保质期name.php 页

<?php

include 'confignew.php';

$name = $_POST['name'];

// peforming database operations
.
.
.

// and then

if($db->num_rows($q) == 0)
{
    echo 'go';
}
else
{
    echo 'stop';
}

我想的东西很明显。希望这里有人能指出这点。

I'm missing something very obvious. Hopefully someone here can point that out.

与萤火虫在Firefox检查后,我的确得到了一个错误。它并没有显示出来,当我测试用Chrome浏览器。下面是截图。

After checking with Firebug in Firefox, I indeed got an error. It didn't show up when I was testing with Chrome. Here is the screenshot.

推荐答案

我测试过你的code和它的作品在Chrome,IE 8和Mozilla Firefox。检查是否在你的整个网页没有包含在它的的name属性值字中的元素提交。如果将其重命名。例如像这样的输入标签:&LT;输入类型=提交名称=提交值=提交表单/&GT; 将导致出现错误的Mozilla的Firebug的。

I've tested your code and it works in Chrome, IE 8 and Mozilla Firefox. Check whether in your whole page there is an element which contains in it's name attribute value the word submit. If there is rename it. For example an input tag like this: <input type="submit" name="submit" value="Submit Form"/> will cause the error appeared in your Mozilla Firebug.

此外,您可以在下面找到一个替代的解决方案。

Furthermore below you can find an alternative solution.

下面的解决方案已经成功在Chrome,IE 8和Mozilla Firefox的测试。

The following solution has been successfully tested in Chrome, IE 8 and Mozilla Firefox.

替代解决方案

检索要发布,并在成功回调执行后的帖子的网址和数据。

Retrieve the post URL and the data that you want to post and perform a post in the success callback.

下面的实现已成功在Chrome,IE 8和Mozilla Firefox 测试。在成功的回调,以公布该数据被检索并发布到URL,并将结果放到一个div id为的结果的。您可以以适合您的需要进行修改。

The following implementation has been successfully tested in Chrome, IE 8 and Mozilla Firefox. In the success callback, the data to be posted is retrieved and posted to the URL and the result is put to a div with id result. You can modify it in order to fit your needs.

$(document).ready(function() {
    $('#form1').submit(function(){
        var name = $('#shelf_name').val();
        if(name == '')
        {
            alert('Shelf name is required');
            $('#shelf_name').focus();
        }
        else
        {
            $.ajax({
                type:'post',
                url:'check-duplicate-shelf-name.php',
                data:{'name':name},
                context:this,
                success:function(data)
                {
                    if(data == 'stop')
                    {
                        alert('Shelf name already exists');
                    }
                    else
                    {   
                        alert('else working?'); 
                        //this.submit();
                        //$(this).submit();

                        // get the post url and the data to be posted
                        var $form = $(this);
                            shelfNameVal = $form.find( 'input[id="shelf_name"]' ).val(),
                            url = $form.attr( 'action' );

                            // Send the form data and put the results in the result div
                            $.post(url, { shelf_name: shelfNameVal },
                                function(data) {
                                    $( "#result" ).empty().append(data);
                                }
                            );
                    }
                }
            });
        }
    return false;
    });
});

我希望这有助于。

I hope this helps.