Jquery提交表单,在成功提交表单到php时添加

时间:2022-11-07 13:25:49

Need help in jquery:

在jquery需要帮助:

here is a schema of code:

下面是一个代码模式:

  1. submit form through jquery to php
  2. 通过jquery向php提交表单
  3. php sends another form upon submission
  4. php在提交时发送另一个表单。
  5. again submit form without page refresh and this continues forever.
  6. 再次提交表单而不刷新页面,这将一直持续下去。

sample code:

示例代码:

$(document).ready(function()
{
    $(".message-submit-button").click(function(){   
        var element = $(this);
        var Id = element.attr("id");
        var messageinput = $("#messageinput"+Id).val();
        var action = $("#action"+Id).val();
        var dataString = 'messageinput='+ messageinput + '&mobilenumber=' + Id + '&action=' + action;

        if(messageinput=='')
        {
            alert("Please Enter Message");
            document.getElementById('messageinput'+Id).focus();
        }
        else
        {
            $("#flash"+Id).show();
            $.ajax({
                type: "POST",
                url: "insert_message.php",
                data: dataString,
                cache: false,
                success: function(html){
                    $("#flash"+Id).hide();
                    $("#message-content"+Id).replaceWith(html); 
                }
            });

        }
        return false;
    });
});

In above script, please note that the output "html" is none other than a same form with different set of ids.

在上面的脚本中,请注意输出的“html”不是别的,而是具有不同id集的相同表单。

above code is working fine for first form, but not for the second or so on. Please help for continues form submission.

上面的代码对于第一种形式是有效的,但是对于第二种形式则不然。请帮助继续提交表格。

Note: Upon submission the form to 'insert_message.php', it returns another form in a div tag with different set of ids but same inputs and classnames.

注意:将表单提交给“insert_message”。它返回div标记中的另一个表单,该表单具有不同的id集,但输入和类名相同。

1 个解决方案

#1


3  

If the form is the same just with different ids, just change the ids. If you must change the form you'll have to re-attach the event handler or you can use delegation

如果表单与不同的id相同,只需更改id。如果必须更改表单,则必须重新附加事件处理程序或使用委托

$(document).on("click", ".message-submit-button", function(){

http://api.jquery.com/on/

http://api.jquery.com/on/

If your not on jquery 1.7+ you can use delegate instead

如果您没有使用jquery 1.7+,您可以使用委托

$(document).delegate(".message-submit-button", "click", function(){

http://api.jquery.com/delegate/

http://api.jquery.com/delegate/

#1


3  

If the form is the same just with different ids, just change the ids. If you must change the form you'll have to re-attach the event handler or you can use delegation

如果表单与不同的id相同,只需更改id。如果必须更改表单,则必须重新附加事件处理程序或使用委托

$(document).on("click", ".message-submit-button", function(){

http://api.jquery.com/on/

http://api.jquery.com/on/

If your not on jquery 1.7+ you can use delegate instead

如果您没有使用jquery 1.7+,您可以使用委托

$(document).delegate(".message-submit-button", "click", function(){

http://api.jquery.com/delegate/

http://api.jquery.com/delegate/