简体   繁体   English

我如何 Append Id 属性选择器或将 Id 存储在 Jquery 变量中并将该变量用作选择器?

[英]How can I Append the Id attribute Selector or Store the Id in Jquery variable and use that variable as selector?

I want to perform show/hide operation on textarea element on clicking the replay button using id as selector in jquery but my id is in the form id = "textarea"+dat.description_id ie dat.description_id is the value that i have concate.我想在使用 id 作为 jquery 中的选择器单击重播按钮时对 textarea 元素执行显示/隐藏操作,但我的 id 格式为id = "textarea"+dat.description_id即 dat.description_id 是我连接的值。 and also I have put this element in loop so there are many textarea field on the page.而且我已经把这个元素放在循环中,所以页面上有很多 textarea 字段。

        -if(data.length > 0)
            each dat in data
              form(action="/update/"+dat.description_id, method="post")
                tr
                  td #{dat.description_id}
                  td #{dat.applied_date}
                  td #{dat.fullname}
                  td 
                  td #{dat.complaint_name}
                  td #{dat.complaint}
                  td
                    div(style="display:flex")
                      -if(dat.status == 'Done')
                        p #{dat.replay}
                      -else
                        textarea(name="replay" id="textarea"+dat.description_id class="form-control" cols="30", rows="2")
                        button(type="submit" id="replay-btn"+dat.description_id  class="btn btn-primary m-2" onclick="myfun()") Reply

By performing the Below Jquery Code there is no event occuring on page.通过执行下面的 Jquery 代码,页面上不会发生任何事件。 According to me if I we can store the Id in the jquery variable we can do that thing but the question is how to use that variable as the selector OR ELSE how to append that dat.description_id in $("textarea") .根据我的说法,如果我们可以将 Id 存储在 jquery 变量中,我们可以做到这一点,但问题是如何使用该变量作为选择器,或者如何使用$("textarea")中的 dat.description_id dat.description_id PLESE tell ME the SOLUTION for this PRoblem......请告诉我这个问题的解决方案......

script.
      $(document).ready(function(){
          $("textarea").hide();
          $('#replay-btn').click(function(){
            $("#textarea").toggle();
          });

          $("#textarea").keyup(function(){
            var len = $(this).val().length;
            if(len > 0)
            {
              $("#replay-btn").attr('type','submit');
            }
            else{
              $("#replay-btn").attr('type','button');
            }
          })
      });

One way to achieve this would be to create a data-controls attribute on the button element that matches the id of the corresponding textarea element.实现此目的的一种方法是在button元素上创建一个与相应textarea元素的id匹配的data-controls属性。 Then you could add a js-reply-button class to the button elements to bind the jQuery events.然后,您可以将js-reply-button class 添加到button元素以绑定 jQuery 事件。

I would recommend rewriting your Pug else statement as follows:我建议重写您的 Pug else语句,如下所示:

else
  textarea.js-reply-textarea.form-control(id=`textarea_${dat.description_id}`, name='reply', cols='30', rows='2')
  button.js-reply-button.btn.btn-primary.m-2(data-controls=`textarea_${dat.description_id}`, type='submit') Reply

With this structure, your jQuery to show/hide each text area when someone clicks the reply button could look like this:使用这种结构,当有人单击回复按钮时,您的 jQuery 显示/隐藏每个文本区域可能如下所示:

$(document).ready(function() {
  // hide the textarea elements
  $('.js-reply-textarea').hide();
  // when a user clicks the reply button
  $('.js-reply-button').click(function() {
    // get the value of the button's data-controls attribute
    let textareaId = $(this).attr('data-controls');
    // use it to target and toggle the right textarea element
    $('#' + textareaId).toggle();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM