English 中文(简体)
使用jquery $.ajax - oly 提交表格
原标题:Submitting forms using jquery $.ajax - oly the first submits

我对提交 AJAX 表格有异议 - 使用 < a href=" http://net.tutsplus.com/tutorics/javascript-ajax/submit- a-form- a-form- non-page-refresh-using-jquery/" rel=“nofollow” > 辅导 。

我的表格在 div#upform 中,当我试图通过 $.ajax 提交其中的任何表格时,它只提交第一个表格,这里的代码是:

$(function() {  
    $(".button").click(function() {  
        var txt = $(".tekst#test").val();  
        var dataString =  tekst= + tekscior;

        $.ajax({  
            type: "POST",  
            url: "upload/base",  
            data: dataString,  
            success: function() {  
                $( #upform ).html("<div id= message ></div>");  
                $( #message ).html("<h2>described!</h2>")  
                    .append("<p>thanks!</p>")  
                    .hide()  
                    .fadeIn(1500, function() {  
                        $( #message ).append("<img id= checkmark  src= http://artivia-dev2/i/check.png  />");  
                    });  
            }  
        });  
        return false; 
    });  
});  

这是我的表格:

<!-- ONLY THIS ONE IS SUBMITTED, EVEN WHEN I M SUBMITTING THE SECOND ONE! -->
<div class="slidingDiv">
    <div id="upform">
        <form name="contact" action="">  
            <input type="text" value="TESTFORM" class="tekst" id="test">
            <input type="submit" name="submit" class="button" id="submit" value="Send" />
        <form>
    </div> 

    <div class="slidingDiv">
        <div id="upform">
            <form name="contact" action=""> 
                <input type="text" value="TESTFORM" class="tekst" id="test">
                <input type="submit" name="submit" class="button" id="submit" value="Send" />
            <form>
        </div> 

<%%UPDATE

问题,当我提交一个表格时——它很好——但当我提交第二个表格后,我想提交第二个表格时——数据提交正确,但成功的信息以两种形式得到刷新,这是我要使用的固定方法,但是它不起作用:

    $.ajax({  
          type: "POST",  
          url: "upload/base",  
          data: dataString,  
          success: function() {  

        upform.html("<div class= message ></div>");  

             var mess =  $(this).closest( .message );

             mess.html("<h2>Described</h2>")  
            .append("<p>Thanks!</p>")  
            .hide()  
            .fadeIn(1500, function() {  
            mess.append("<img id= checkmark  src= http://ar-dev2/i/check.png  />");  
            });  


          }  
        }); 
最佳回答

首先,您不应对多个元素使用相同的 id 。 您可以使用 class name data 属性。

$(".button").click(function() {  

   var upform = $(this).closest( .upform ); // keep reference of upform

    var txt = $(this).prev(".tekst").val();  // this will retrieve the value of input
                                             // nearest to the button

    var dataString =  tekst= + tekscior;

    ......

    $.ajax({
      type: "POST",  
      url: "upload/base",  
      data: dataString,
      success: function() {
        upform.html();
        .....
      }
    });
});

Problem with var txt = $(".tekst#test"); selector:

它开始从顶部搜索, 当它发现匹配时, 当它发现匹配时, 它停止旅程, 返回值, 然后返回第一个值, 您总是得到第一个值 。 如果您使用

var txt = $ ("tekst"); ,没有 id ,你将面临同样的问题。

问题回答

身份识别码是单数的。 您不能在页面上重复使用相同的身份识别码!

如果您想要重复标识符,请使用名称。

您应该对两种窗体使用不同的代号。 您的两种窗体都有相同的 ID appform





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签