2

我是 JQuery 的新手,我不敢相信这是不可能的......

我有多个动态创建的 HTML 表单。我正在使用 JQuery 从表单中捕获数据,但是当我提交表单时,我只能访问最后一个表单中的数据。

这是我的代码:

for($x=0; $x<10; $x++){

     ?>
     <form method="POST" id="formid<? echo $x; ?>">
      <textarea name="notes_text"><? echo $row[2]; ?></textarea></td>
      <input type="hidden" value="<? echo $row[0]; ?>" name="notes_id" />
      <input type="image" src="/images/edit-small.png" id="update-notes-submit" class="submit-img-sml"/>            
     </form>
     <?

}

和 jQuery:

$("form").submit(function(){

     var form_id = $(this).closest("form").attr('id');  
     alert(form_id);

     var notes_id=$('#formid1 input[name=notes_id]').val();
     var notes_text=$('#formid1 textarea[name=notes_text]').val();

}

警报给了我正确的形式,这样就可以了。

如果我提醒 notes_id 或 notes_text 变量,我会得到“未定义”。

我努力了:

var notes_id=$('#formid1 input[name=notes_id]').val();
= undefined

var notes_id=$('#formid2 input[name=notes_id]').val();
= undefined

var notes_id=$('input[name=notes_id]').val();
= data from the last form that appears in my script

如何指定表单和变量。我不敢相信这很难实现,也许 JQuery 不可能做到这一点,如果是这样,这是一个令人震惊的限制。

4

4 回答 4

3

您假设表单 id 是formid1,但我感觉不是正在提交的那个。尝试以下操作:

$("form").submit(function(){
     var form =$(this);//this refers to the form
     var notes_id=form.find('input[name=notes_id]').val();
     var notes_text=form.find('textarea[name=notes_text]').val();
 }
于 2013-02-09T01:37:05.463 回答
1

Ok so I finally figured it out after about 8 hours of hammering away.

The problem was caused by the table tags which were in the wrong place. I was using this format:

<table>

 <?

 for($x=1; $x<=mysql_num_rows($notes); $x++){

      $row=mysql_fetch_row($notes);

      ?>
      <form id="form<? echo $x; ?>">

       <tr>
        <td width="150"><b>Text 1</b></td>
        <td colpsan="3"><textarea name="txtarea">Some text</textarea></td>
       </tr>

      <input type="hidden" value="<? echo $row[0]; ?>" name="notes_id" />        
      </form>

      <?

 }

?>

</table>

But if I placed the table opening and closing tags within the for loop it solved the problem:

 <?

 for($x=1; $x<=mysql_num_rows($notes); $x++){

      $row=mysql_fetch_row($notes);

      ?>

      <table>
       <form id="form<? echo $x; ?>">

        <tr>
         <td width="150"><b>Text 1</b></td>
         <td colpsan="3"><textarea name="txtarea">Some text</textarea></td>
        </tr>

       <input type="hidden" value="<? echo $row[0]; ?>" name="notes_id" />        
       </form>

      </table> 

      <?       

}

?>

I'm not sure why this works, because as far as I can see this simply creates a table for each form, where as previously a single table was used.

Thank you so much for your help - it is appareciated!

于 2013-02-11T18:10:03.180 回答
0

去掉多余的</td></textarea>。它与表单外部匹配,因此表单元素也被终止。结果,元素不在表单内。看看检查器中的 DOM,你会发现这是多么混乱。

于 2013-02-09T02:32:18.527 回答
-3

你不是缺少引号吗?

var notes_id=$('#formid1 input[name=notes_id]').val();
var notes_text=$('#formid1 textarea[name=notes_text]').val();

var notes_id=$('#formid1 input[name="notes_id"]').val();
var notes_text=$('#formid1 textarea[name="notes_text"]').val();

很确定这些报价很重要。

于 2013-02-09T01:48:22.923 回答