doulu7921 2017-11-13 09:50
浏览 232
已采纳

同一页面上的多个有用按钮

I've made a "Useful-Button" with jQuery so user can rate e.g. an answer of a question (like the upvote function here on stackoverflow).

function submitForm() {
    var form = document.myform;
    var dataString = $(form).serialize();
    
    $.ajax({
        type:'POST',
    url:'like_submit.php',
    data: dataString,
    success: function(data){
        $('#myResponse').html(data);
        $('#myResponse').fadeIn().delay(3000).fadeOut();
    }
   });
   return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform" class="myform" method="post" name="myform">
    Was this 1. answer helpful?
  <input type="hidden" name="answer_id" value="1" />
  <input  class="label__checkbox" type="checkbox" name="helpful" onclick="submitForm()" />
</form>
<div id="myResponse"></div>

The like_submit.php is like this:

<?php
$helpful = $_POST['helpful'];
$answer_id = $_POST['answer_id'];
echo "Thank you! Helpful? $helpful | AnswerID: $answer_id";
?>

So for one answer it works.

But now I want to add more than one answer and I doesn't know how get the script to work for this case.

If I add a second form...

<form id="myform" class="myform" method="post" name="myform">
Was this 2. answer helpful?
  <input type="hidden" name="answer_id" value="2" />
  <input  class="label__checkbox" type="checkbox" name="helpful" onclick="submitForm()" />
</form>
<div id="myResponse"></div>

... I have two problems. I only get the last POST-data right. If I click the first form, it says: Thank you! Helpful? on | AnswerID: 2

Second problem, the response message appears allways an the first position.

Can anyone help? Many thanks!

</div>
  • 写回答

1条回答 默认 最新

  • dongxin991209 2017-11-13 09:54
    关注

    Get the elements relative to the submitted form

    html: onclick="submitForm(this)"

    js:

    function submitForm(el) {
        var form = $(el).closest('form');
        var dataString = form.serialize();
    
        $.ajax({
            type:'POST',
        url:'like_submit.php',
        data: dataString,
        success: function(data){
            form.next('#myResponse').html(data);
            form.next('#myResponse').fadeIn().delay(3000).fadeOut();
        }
       });
       return false;
    }
    

    function submitForm(el) {
        var form = $(el).closest('form');
        var dataString = form.serialize();
            form.next('#myResponse').html(dataString);
            form.next('#myResponse').fadeIn().delay(3000).fadeOut();
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="myform" class="myform" method="post" name="myform">
        Was this 1. answer helpful?
      <input type="hidden" name="answer_id" value="1" />
      <input  class="label__checkbox" type="checkbox" name="helpful" onclick="submitForm(this)" />
    </form>
    <div id="myResponse"></div>
    <form id="myform" class="myform" method="post" name="myform">
    Was this 2. answer helpful?
      <input type="hidden" name="answer_id" value="2" />
      <input  class="label__checkbox" type="checkbox" name="helpful" onclick="submitForm(this)" />
    </form>
    <div id="myResponse"></div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多