简体   繁体   English

如何使用Jquery在单击的div内获取输入标签的值?

[英]How to get the value of input tag inside the clicked div using Jquery?

I have a following Ajax success function which prints the folder div in the DOM when user load the page or add the folder. 我有以下Ajax成功函数,当用户加载页面或添加文件夹时,该函数将在DOM中打印文件夹div。

for(var i in output.dirs){
        //console.log(output.dirs[i]);
        var html = '<div class="col-sm-3 text-center" id="img-folder"><input type="hidden" id="folder-names" name="open-folder" value="'+output.dirs[i].name+'"/>';
            html+='<div class="mid-folder">  <i class="fa fa-folder fa-5x"></i>';
            html+='<h3 class="title-folder">'+output.dirs[i].name.substr(0,15)+".."+'</h3></div>';
            $(".folders").append(html);

And I want to add yet another Ajax request when each folder is clicked. 我想在单击每个文件夹时添加另一个Ajax请求。 I want to send the folder name of the folder that is being clicked and send it through the Ajax to the PHP controller, so that I could retrieve the images inside that folder. 我想发送被单击的文件夹的文件夹名称,并将其通过Ajax发送到PHP控制器,以便可以检索该文件夹中的图像。 For that I attached an input tag with hidden attribute to print out the folder name as value, so that it could be easy for me to get the value using .val(). 为此,我附加了具有隐藏属性的输入标签,以将文件夹名称作为值打印出来,以便我可以轻松地使用.val()获取值。

But the problem here is that how could I know which folder has been clicked and what's the value of the <input> tag which belongs to that div, since every div printed will have the same id "img-folder". 但是这里的问题是,我怎么知道单击了哪个文件夹以及属于该div的<input>标记的值是什么,因为每个打印的div都具有相同的ID“ img-folder”。

Looking at some alternatives, I found this: 查看一些替代方案,我发现了这一点:

$("#img-folder").each(function(i){
$(this).on("click",function(){
   // Now how would I select the <input> tag value here, so that I could    pass the value into the controller? 
});
});

What I want to do now is to catch the value/folder name of the folder that being clicked and send the value to my ajax function which is something like this: 我现在想要做的是捕获被单击的文件夹的值/文件夹名称,并将该值发送到我的ajax函数,如下所示:

// function for showing the images and contents inside the folders. 
  $(function(){
    $(document.body).on('click',"#img-folder",function(e){
     console.log($("#folder-names").val());
      //e.preventDefault();
      $.ajax({
        url:'<?php echo base_url("Welcome/show_image"); ?>',
        type:'POST',
        data:$('#folder-name').val(),
        success:function(data){
          console.log(data);

        },
      });
    })
  })

Any suggestion or any other logic to solve this problem? 有什么建议或其他逻辑可以解决这个问题? It would be great. 这会很棒。

You are duplicating ids of element. 您正在复制元素的ID。 IDs must be unique. ID必须是唯一的。 You can rather give same class. 你宁可给同一个班级。 like this: 像这样:

for(var i in output.dirs){
    //console.log(output.dirs[i]);
    var html = '<div class="col-sm-3 text-center" class="img-folder"><input type="hidden" class="folder-names" name="open-folder" value="'+output.dirs[i].name+'"/>';
        html+='<div class="mid-folder">  <i class="fa fa-folder fa-5x"></i>';
        html+='<h3 class="title-folder">'+output.dirs[i].name.substr(0,15)+".."+'</h3></div>';
        $(".folders").append(html);

and then use class selector to find element with class folder-names in clicked img-folder : 然后使用类选择器在单击的img-folder查找具有类folder-names元素:

 $(function(){
$(document.body).on('click',".img-folder",function(e){
 var folderval = $(this).find(".folder-names").val();
 console.log($(this).find(".folder-names").val());
  //e.preventDefault();
  $.ajax({
    url:'<?php echo base_url("Welcome/show_image"); ?>',
    type:'POST',
    data:folderval ,
    success:function(data){
      console.log(data);

    },
  });
 });
});

This is what you need - 这就是您需要的-

Using jQuery this , you will get attribute value of that element. 使用jQuery this ,您将获得该元素的属性值。 So, in your case it is input . 因此,在您的情况下为input You will store that attribute value and pass it in AJAX call. 您将存储该属性值并将其传递给AJAX调用。

$(this).on("click",function(){
   var inputValue = $(this).attr('value');
        $.ajax({
    url:'<?php echo base_url("Welcome/show_image"); ?>',
    type:'POST',
    data: inputValue ,
    success:function(data){
      console.log(data);

    },
  });
});

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

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