2013-08-29 63 views
0

我有這個代碼中發現父母爲形式的問題:如何找到父母?

<li class="comment<?php echo $comment[id]?>"> 
    <div class="comment-content"> 
     <div class="comment-top"> 
      <div class="comment-nme"> 
        <?php echo $comment[name]?> 
      </div> 
       <div class="comment-dt"> 
        <?php echo $comment[dt]?> 
       </div> 
     </div> 
     <div class="comment"> 
      <?php echo $comment[comment]?> 
     </div> 
     <a class="reply" href="#comment<?php echo $comment[id]?>">Ответить</a> 
    </div> 
    <div class="answer-form"> 
     <form method="post" name="answer-form" class="ans"> 
      <textarea class="comment-textarea" name="comment"></textarea> 
      <div class="a-comment-inputs"> 
       <input type="hidden" name="parent_id" value="<?php echo $comment[id]?>"> 
       <input type="hidden" name="status" value="new"> 
       <div class="a-comment-name"> 
        Имя</br> 
        <input type="name" name="name" class="a-comment-name"> 
       </div> 
       <div class="a-comment-email" > 
        Eмейл</br> 
        <input type="email" class="a-comment-email" name="email"> 
       </div> 
      </div> 
      <div class="comment-apply"> 
       <button value="submit" onclick="return sendDataChild();" class="answer-but">Добавить</button> 
      </div> 
     </form> 
    </div> 

    <?php if($comment[childs]){ ?> 

     <ul class="commentsRoot<?php echo $comment[id]?>"> 
      <?php echo commentsString($comment[childs]) ?> 
     </ul> 

    <?php } ?> 

</li> 

我使用jQuery的功能:

function sendDataChild() { 
    var form = $('FORM[name=answer-form]'); 
    var data = form.serialize(); 
    $.ajax({ 
     type: "POST", 
     url: "req.php", 
     dataType: "json", 
     data: data, 
     cache: false, 
     success: function (data) { 
      form[0].reset(); 
     }, 
     error: function (xhr, str) { 
      alert('Возникла ошибка: ' + xhr.responseCode); 
     } 
     //$("#messageModalDialog").text(resultStat).show(); 
    }); 
    return false; 
}; 

但它選擇的每形式找到按鈕的點擊。 有人可以建議如何解決它?

+0

我很抱歉不瞭解U – myrko

+1

是否所有的表格都有名稱=答案表單? – Rishabh

+0

是的,我使用遞歸函數&它會生成幾個具有相同名稱的表單 – myrko

回答

0

一個可能的解決方案

<button value="submit" onclick="return sendDataChild(this);" class="answer-but">Добавить</button> 

然後

//pass the clicked button reference then find the parent form of the button 
function sendDataChild(btn) { 
    var form = $(btn).closest('FORM[name=answer-form]'); 
    var data = form.serialize(); 
    $.ajax({ 
     type: "POST", 
     url: "req.php", 
     dataType: "json", 
     data: data, 
     cache: false, 
     success: function (data) { 
      form[0].reset(); 
     }, 
     error: function (xhr, str) { 
      alert('Возникла ошибка: ' + xhr.responseCode); 
     } 
     //$("#messageModalDialog").text(resultStat).show(); 
    }); 
    return false; 
}; 
+0

謝謝!!!! 它的工作原理! – myrko

+1

@downvoter任何可能的原因......這是可能的解決方案之一,而不會改變太多的OP有...也鼓勵閱讀[投票下載](http://stackoverflow.com/help/privileges /投票下來),特別是當投票下來時 - 每當你遇到一個極其潦草,沒有付出努力的帖子,或者一個明確而且可能危險不正確的答案時,使用你的低評分。' –

0

綁定的形式提交事件和對象到窗體對象傳遞給你的方法

$(document).ready(function(){ 
    $("form[name='answer-form']").on('submit', function(){ 

     sendDataChild($(this)); 

    }); 
}); 

function sendDataChild(form) { 
    var data = form.serialize(); 
    $.ajax({ 
     type: "POST", 
     url: "req.php", 
     dataType: "json", 
     data: data, 
     cache: false, 
     success: function (data) { 
      form.reset(); 
     }, 
     error: function (xhr, str) { 
      alert('Возникла ошибка: ' + xhr.responseCode); 
     } 
     //$("#messageModalDialog").text(resultStat).show(); 
    }); 
    return false; 
}; 
+0

這個函數dosnt工作 – myrko