【问题标题】:Get variable inside certain div with jquery, when there are multiple divs with same id当有多个具有相同 id 的 div 时,使用 jquery 在某个 div 内获取变量
【发布时间】:2019-04-15 16:53:52
【问题描述】:

我正在用 jsp 打印一个数组列表。该数组列表中的每个对象都使用如下循环打印:

                        <% ArrayList <MessageObject> list = (ArrayList<MessageObject>) request.getAttribute("list"); %>


                                <%int index = 0;%>
                                <%for(MessageObject msg :list){
                                    index++;
                                    if(mensaje.getState().compareTo("unread") == 0){%>
                                    <tr data-status="unread" class="unread">
                                        <td>
                                            <a href="javascript:;" class="star">
                                                <i class="glyphicon glyphicon-star"></i>
                                            </a>
                                        </td>
                                        <td>
                                            <div class="media">
                                                <h4 class="title">
                                                            User Identifier
                                                </h4>
                                            </div>
                                        </td>                                        
                                        <td id="unread-id">      
                                                <div class="media">
                                                    <p class="summary"><% out.print(msg.getMessage());%></p>

                                                    <input id="index" type="text" value="<%out.print(index);%>"></input>

                                                </div>

上面没有写一些结束标签和其他结构,是为了让我的代码更容易阅读。
基本上,它会打印来自队列的消息,以及它在数组列表中的索引:

我的问题是,当我点击任何消息时,我想保存它们的索引值。

我试过了:

<script>
      $(document).on('click', '#unread-id', function () {
          var index = $('#index').val();
          $("#setindex").val(index);
       });

所以我点击任何包含消息的 div,脚本被调用,但我总是得到相同的索引值,1。 问题是始终具有相同 id 名称的相同 div,导致我的脚本总是选择第一个 id 为 unread-id 的 div,它始终是第一个,所以它返回 1。

如果我所有的容器 div 都具有相同的 id 值,我如何获取点击的 div 的索引?

【问题讨论】:

    标签: javascript java jquery spring jsp


    【解决方案1】:

    向您的&lt;td id="unread-id"&gt; 添加一个类,例如row,并将您的脚本更改为以下。你的td 最终应该看起来像&lt;td class="row"&gt;。另外,不要在输入中使用 id,将其更改为一个类,例如 row-input

    JS

    $(document).on('click', '.row', function () {
          var index = $(this).find('.row-input').val();
          $("#setindex").val(index);
    });
    

    JSP 更改

    • &lt;td id="unread-id"&gt;&lt;td class="row"&gt;
    • &lt;input id="index" type="text" value="&lt;%out.print(index);%&gt;"&gt;&lt;/input&gt;&lt;input class="row-input" type="text" value="&lt;%out.print(index);%&gt;"&gt;&lt;/input&gt;

    注意

    您正在为所有行设置相同的id。 id 必须是唯一的,这就是您不断获得相同索引的原因。

    【讨论】:

      【解决方案2】:

      First - id 在您的页面中应该是唯一的。你真的应该解决这个问题(如果你需要一些选择器来处理多个元素 - 你可以改用类名)。

      但是 - 您的代码可以工作(可能会导致某些浏览器出现问题,所以我真的建议您尽快修复此问题):

      $(function() {
        $(document).on('click', '#unread-id', function () {
          console.log($(this).val());
        });
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <input id="unread-id" value="1" /><br />
      <input id="unread-id" value="2" /><br />
      <input id="unread-id" value="3" /><br />
      <input id="unread-id" value="4" /><br />

      当在 click 函数内时 - this 元素是您刚刚单击的元素。您可以使用它来获得所需的价值。

      【讨论】:

      • 好吧,id 应该是唯一的。这已经是个问题了。
      • @AlainCruz 完全同意你的看法,我的回答中也提到了这一点。但是 - 即使在这种情况下 - 这也可以(在当前浏览器中)。该解决方案将来可能无法正常工作,因此我真的建议使用 class 而不是在整个页面上复制 id。
      • 和顺便说一句-如果您在阅读我的答案后$(this) 的用法不太好:)
      • 你知道,我只在他的td 中看到了第一个重复的 id,直到我意识到他的input 中还有另一个 id 哈哈哈 :-)
      • 嗯,我赞成,因为你确实展示了它。不过,您的问题仍然缺少 td 的 id 问题,他/她应该解决这个问题。
      猜你喜欢
      • 2018-12-27
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多