1

我有任何回答/问题的页面。我检索管理员的问题列表。管理员看到这个。现在我需要发送每个问题的答案。所以我为每个问题提出textarea并形成后行动。现在我需要在发送问题消息的答案时,如果将消息发送到外部 php 文件 = true;消息(ID)删除(jquery 上滑效果)。为此,我有 jquery 提交表单代码(没有刷新页面),但我有大问题。这仅适用于一种形式!并且不适用于所有列表形式(问题+答案形式)。如何将我的代码用于多种形式?我选择了正确的方式?

html代码:

<form action="insert.php?id=42" id="forms" method="POST" name="form">
<div id="box">
<div class="messagequestion"></div>
<div class="messagereply"><textarea></textarea><input type="submit" class="submit" name="submit" value="submit"></div>
</div>
</form>
<form action="insert.php?id=45" id="forms" method="POST" name="form">
<div id="box">
<div class="messagequestion"></div>
<div class="messagereply"><textarea></textarea><input type="submit" class="submit" name="submit" value="submit"></div>
</div>
</form>
<form action="insert.php?id=48" id="forms" method="POST" name="form">
<div id="box">
<div class="messagequestion"></div>
<div class="messagereply"><textarea></textarea><input type="submit" class="submit" name="submit" value="submit"></div>
</div>
</form>
<form action="insert.php?id=50" id="forms" method="POST" name="form">
<div id="box">
<div class="messagequestion"></div>
<div class="messagereply"><textarea></textarea><input type="submit" class="submit" name="submit" value="submit"></div>
</div>
</form>

谢谢

4

3 回答 3

0

Your major issue is probably that you are trying to reuse ids. All the forms have the id of "forms" and you are also sharing the id "box".

All ids should uniquely identify an element. Use a class when you need to classify an element. I'd recommend you change id="forms" on all the forms to class="reply_form" and then also change id="box" on all the divs to class="reply_box". Then change styles set for #forms and #box to those set for .reply_form and .reply instead.

EDIT - tweaks made in jsfiddle after some discussion with the OP.

http://jsfiddle.net/gvnfg/5/

于 2012-04-06T12:45:53.270 回答
0

只需更改选择器。id属性在 html 中必须是唯一的

$("[name='form']").submit(function() {
    $this = $(this);    
    $.ajax({
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        cache: false,       
        beforeSend: function() {
            $('#loading').show();
            $('#result').hide();
        },
        success: function(data) {
            if(data==1){
              $('#loading').hide();
              $('#result').fadeIn('slow').html("ok");
              $('#result').addClass('true');
              $this.slideUp(1000);
            }
        else {
            $('#loading').hide();
            $('#result').fadeIn('slow').html(data);
            $('#result').addClass('errors');
        }}
    });
     e.preventDefault();
    return false;
});
于 2012-04-06T12:49:12.880 回答
-1

使用 Jquery.each()方法:

$(document).ready(function() {
    $('#forms').each(function() {

       this.submit(function() {
           $.ajax({ ... });
       });
    });

})
于 2012-04-06T12:46:49.073 回答