Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个动态分配的id使用循环只显示第一个分配?javascript、AJAX和JSTL_Javascript_Java_Jquery_Jstl - Fatal编程技术网

为什么这个动态分配的id使用循环只显示第一个分配?javascript、AJAX和JSTL

为什么这个动态分配的id使用循环只显示第一个分配?javascript、AJAX和JSTL,javascript,java,jquery,jstl,Javascript,Java,Jquery,Jstl,有一个基于Java的web应用程序,该应用程序的页面在JSTL的帮助下动态生成文章提要。用户目前可以“喜欢”提要中的任何帖子,但事实证明,使用AJAX实现这一点要困难得多。我知道我离这里很近,但我不太清楚到底出了什么问题 它只适用于数组中的第一项。。因此,在提要中按下的任何like按钮都只会更新提要中的第一个like按钮。为什么动态分配的div值input name=likesDivCount只注册第一次分配 index.jsp <c:forEach items="${idFeedArra

有一个基于Java的web应用程序,该应用程序的页面在JSTL的帮助下动态生成文章提要。用户目前可以“喜欢”提要中的任何帖子,但事实证明,使用AJAX实现这一点要困难得多。我知道我离这里很近,但我不太清楚到底出了什么问题

它只适用于数组中的第一项。。因此,在提要中按下的任何like按钮都只会更新提要中的第一个like按钮。为什么动态分配的div值input name=likesDivCount只注册第一次分配

index.jsp

<c:forEach items="${idFeedArray}" var="posts" varStatus="count">

...feed item (such as image, text etc..)...

<form id="likesform" action="../AddLike"  method="post" style="display:inline;">

  // the value of this input below is supposed to change...(i.e. #likesSize0,#likesSize1,#likesSize2)
   <input name="likesDivCount" value="#likesSize${count.index}" type="hidden">
   <input name="postUser" value="${userpost[count.index]}" type="hidden">


  // this button sends the AJAX request 
  <button style="padding-right: 0;"  type="submit" class="btn btn-link"><span class="glyphicon glyphicon-thumbs-up"></span></button>

</form>

    // The span in the button below updates with the response from the AJAX
    <button style="padding-left: 0;" class="btn btn-link"><span id="likesSize${count.index}">${likesArraySize[count.index]}</span></button>


</c:forEach>


  <script>

       $(document).on("submit", "#likesform", function(event) {
            var $form = $(this);
            var likesDivCount = $("input[name=likesDivCount]").val();

           //this alert below is for testing, everytime the like button is pressed it displays '#likesSize0' and i need it to spit #likesSize1,#likesSize2,#likesSize3 etc...

            alert(likesDivCount); 

            $.post($form.attr("action"), $form.serialize(), function(response) {      

            // only updates the first item :( (#likesSize0)
            $(likesDivCount).text(response);  


            });

            event.preventDefault(); // Important! Prevents submitting the form.
        });
 </script>

看起来您有多个具有相同ID的表单:“likesform”。这是因为表单是在循环中生成的

我建议您删除ID,将其替换为css类或其他内容,并重写JS以搜索目标表单中的元素,例如:

var $form = $(this);
var likesDivCount = $form.find("input[name=likesDivCount]").val();

一旦有了有效的html,就可以更容易地进行故障排除了

Hmm,但是表单工作得非常好,我可以动态地获取提要中每个帖子的值,并从servlet中获得我想要的响应。因此,似乎拥有一个动态命名的表单不会改变任何事情。或者解决这个问题。编辑-我刚刚测试了你的解决方案,但没有解决问题。你说:它可以工作,但只适用于阵列中的第一项。。因此,在提要中按下的任何like按钮都只会更新提要中的第一个like按钮。我相信这是因为具有相同ID的多个表单。作为一般原则,您永远不应该在同一个HTML文档中复制ID,框架也不应该进行测试。我的建议是:首先让你的html是有效的,然后如果它不起作用,看看其他的东西,但你可以排除它是因为无效的html我的坏,玩了它一些,并使用一个唯一的名称修复它。谢谢