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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵