简体   繁体   English

表单数据未发布到$ .ajax URL

[英]Form data not being posted to $.ajax url

When posting my form from a custom CSS modal the data is being posted to the current URL and not the set url. 从自定义CSS模式发布表单时,数据将发布到当前URL,而不是设置的URL。

$("form.delete_photo_form").submit(function(e) 
{
    e.preventDefault();

    $(".delete_photo_message").text("");


    $.ajax({
        url: 'assets/php/delete_photo.php',
        type: 'post',
        data:  $(this).serialize(),
        success: function(data, status) 
        {
            if (data == 1)
            {
                $(".delete_photo_message").text("Unable to delete photo");
            }                           
            else if (data == 2)
            {
                $(".delete_photo_message").text("Photo deleted");
                // checkGroup();
            }                           
            else
            {
                $(".delete_photo_message").text(data);  
            }
      },
      error: function(xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
      }
    }); 

This form is being posted from a modal opened by another modal. 此表单是从另一个模式打开的模式中发布的。

Original modal: 原始模式:

<div id="photo_buttons">

<a id="edit_photo_modal" class="button" href="#edit_photos_modal">

    Edit Photos

</a>
<div id="edit_photos_modal" class="modal_photos">
    <div>
        <a id="close_edit_photo" title="Close" href="#close"></a>


                                Edit Photos:


        <br></br>
        <br></br>
        <div class="edit_photos_div">
            <div class="photo_line">
                <img src="images/thumbnails/group/11/bb0878d2390cdcfb.jpg"></img>
                <br></br>
                <a id="edit_photo" class="open_modal" href="#modal_edit_photo_39">

                    Edit

                </a>


                                      /  


                <a id="delete_photo" href="#modal_delete_photo_39">

                    Delete

                </a>
                <br></br>
                <br></br>
            </div>                
        </div>
        <a id="finish_edit" class="button" title="Finish" href="#finish">

            Finish

        </a>
    </div>
</div>
<script src="assets/js/edit_photo.js"></script>

Second Modal (this one is where the ajax post is called): 第二模态(这是调用ajax帖子的地方):

    <div id="edit_modals">
    <div id="modal_edit_photo_39" class="modal">
        <div>
            <a id="close_39" title="Close" href="#close">

                Close

            </a>
            <form id="edit_photo_form_39" class="edit_photo_form" method="post" action="">
                <h4>

                    Edit Photo

                </h4>
                <br></br>
                <h3 id="edit_photo_message" class="edit_photo_message"></h3>



                                            Title:

                <input id="edit_title" type="text" value="Test photo" name="edit_title"></input>



                                            Description:

                <textarea id="edit_desc" name="edit_desc" rows="3">

                    Test for resize

                </textarea>
                <br></br>
                <input type="hidden" value="11" name="group_id"></input>
                <input type="hidden" value="39" name="photo_id"></input>
                <input id="edit_photo_yes_39" class="edit_yes" type="submit" title="Edit" value="Edit" name="edit_yes"></input>
                <a id="cancel_edit" class="button" title="Cancel" href="#cancel_edit">

                    Cancel

                </a>
            </form>
        </div>
    </div>
    <div id="modal_delete_photo_39" class="modal">
        <div>
            <a id="close_39" title="Close" href="#close">

                Close

            </a>
            <form id="delete_photo_form_39" class="delete_photo_form" method="post" action="">
                <h4>

                    Are you sure you want to delete this photo?

                </h4>
                <br></br>
                <h3 id="delete_photo_message" class="delete_photo_message"></h3>
                <input type="hidden" value="11" name="group_id"></input>
                <input type="hidden" value="39" name="photo_id"></input>
                <input id="delete_photo_yes_39" class="delete_yes" type="submit" title="Yes" value="Yes" name="delete_yes"></input>
                <a id="del_no" class="button" title="No" href="#no">

                    No

                </a>
            </form>
        </div>
    </div>        
</div>

If anybody has any ideas please let me know. 如果有人有任何想法,请告诉我。

Side note: 边注:

The form submit function works fine if the second modal is called on its own and not from the first. 如果第二个模式本身而不是第一个模式被调用,则表单提交功能可以正常工作。

if you're not going to submit the form at all why don't use onclick function of your button and there in the method call the ajax. 如果您根本不提交表单,为什么不使用按钮的onclick函数,并且在该方法中调用ajax。

Try to take every value and send all of them to check if they have something 尝试获取每个值并将其发送给所有人,以检查它们是否有东西

It's possible that you're either dynamically creating the form after the $("form.delete_photo_form") selector has run, or the modal script could be moving the form's location in the DOM (ie, some modal scripts move the modal content to the very end of the <body> tag). 您有可能在$("form.delete_photo_form")选择器运行动态创建表单,或者模式脚本可能正在DOM中移动表单的位置(即,某些模式脚本将模式内容移动到了DOM)。 <body>标签的结尾)。

In either case, this would cause your submit logic not to run. 无论哪种情况,这都会导致您的提交逻辑无法运行。 Try replacing: 尝试更换:

$("form.delete_photo_form").submit(<handler>);

...with: ...有:

$(document).on("submit", "form.delete_photo_form", <handler>);

If you're not familiar with the above syntax, it's jQuery's syntax for event delegation , which allows you to bind the event handler to something permanent (like the document object), and then selectively run handler logic if the event meets certain criteria (in this case, the event has to be of type "submit" and must have originated from within an element matching the "form.delete_photo_form" selector). 如果您不熟悉上述语法,那么它是事件委托的jQuery语法,它使您可以将事件处理程序绑定到永久性对象(如document对象),然后在事件满足特定条件时有选择地运行处理程序逻辑(在在这种情况下,事件的类型必须为“提交”,并且必须源自与"form.delete_photo_form"选择器匹配的元素内)。 This has the advantage of letting you set up the event listener even if the relevant DOM nodes haven't been created yet. 这样做的优点是,即使尚未创建相关的DOM节点,也可以设置事件侦听器。

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

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