【问题标题】:get value of dynamically created input获取动态创建的输入的值
【发布时间】:2018-12-16 16:06:04
【问题描述】:

这是根据数据库中的信息自动生成的代码

               each p in posts              
                .w3-container.w3-card.w3-black.w3-round.w3-margin
                    p= p.title
                    input(type='hidden', id="idPost" value=p._id)
                    if p.picture
                        img.w3-margin-bottom(src="images/"+p.picture style="width:100%")
                    p= p.content
                    .w3-container.w3-card(id="commentSection") 
                        if p.comments
                            each m in p.comments
                                p= m.user.split(":")[1] + "        "+ m.message
                    textarea(id="NewComment", cols="80%", rows="1" placeholder="Enter a new comment")

我正在尝试获取输入 idPost 的值,但到目前为止并不幸运。我尝试了一些方法,例如$('[id=idPost]').eq(2).val(),但在这种情况下,我需要知道它之前的索引,而我不需要。

我也尝试过类似的东西,我认为这是我想要的,但我没有让它工作

var num = $(this).closest('w3-container').find(".idPost").val();

我正在搜索类而不是上面代码中显示的 id,但这些都不起作用

【问题讨论】:

  • ID 必须是唯一的
  • .find(".idPost") 查找名为 idPost 的类,但没有这样的类
  • 这就是我在最后一句中所说的。我尝试了 id(#idPost) 和 class(.idPost)。都没有工作
  • $('input[type=hidden]').val()
  • @Mirko Acimovic 也没有工作。无论我选择什么,都只需返回相同的元素

标签: jquery pug


【解决方案1】:

input(type='hidden', id="idPost" value=p._id)

这里 idPost 是输入 id 所以你需要找到#idPost

var num = $(this).parents('.w3-container').find("#idPost").val();

【讨论】:

    【解决方案2】:

    因为这两种解决方案都没有我尝试过并且成功了

    哈巴狗

    each p in posts              
                .w3-container.w3-card.w3-black.w3-round.w3-margin
                    p= p.title 
                    if p.picture
                        img.w3-margin-bottom(src="images/"+p.picture style="width:100%")
                    p= p.content
                    .w3-container.w3-card(class="commentSection" name=p._id) 
                        if p.comments
                            each m in p.comments
                                p= m.user.split(":")[1] + "        "+ m.message
                    textarea(id="NewComment", cols="80%", rows="1" placeholder="Enter a new comment")
    

    现在在我的 jquery 中,我只获取当前的 textarea,我按下“enter”,然后将循环所有带有类 commentSection 的“容器”,并检查当前 textarea 的属性名称是否与正确的 commentSection 匹配。如果属实,那么我在正确的地方,可以做剩下的事情。

    $(document).on('keydown', function(e) {
        var targetInput = $(e.target);
          if(targetInput.is('textarea')) { 
             if(e.which == 13){ 
               e.preventDefault();
               $('.commentSection').each(function() {
                   if($(this).attr("name")==targetInput.attr('name')){
                    $(this).append('<p>'+targetInput.val()+'</p>');
                   }
                }); 
              // $("#commentSection").append('<p>'+targetInput.val()+'</p>');
               ajaxPost(targetInput.val(),targetInput.attr('name'),
               p=> alert(JSON.stringify(p)),
               e =>{
                   alert('AJAX ERROR:'+JSON.stringify(e));
                   console.log("AJAX ERROR:"+JSON.stringify(e));
               });
               targetInput.val('');
          }
           }
       });  
    

    有点杂乱无章,但它有效

    【讨论】:

      猜你喜欢
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2020-09-29
      • 2019-05-03
      相关资源
      最近更新 更多