简体   繁体   English

如何获取具有相同类名且使用Jquery动态创建的span标记值?

[英]How can I get the span tag value which has the same class name and Created dynamically using Jquery?

I want to get the span tag text which I created dynamically at runtime. 我想获取我在运行时动态创建的span标签文本。

My task is I want to get the file path name 我的任务是我要获取文件路径名

 $(document).on('click', '.MainFolder', function () { // Your Code var $self = $(this); var parentPath = $(this).children('.pathValue').text(); alert('childrent = ' + $(this).children('.pathValue').text()); $('.SubFolder').css("background", "none"); $('#ContentPlaceHolder1_txtPath_I').val(parentPath); $.ajax({ type: "POST", contentType: "application/json;charset=utf-8", url: "adminCopyCrystalReport.aspx/getDirectoryNames", data: JSON.stringify({ "dirLocation": $(this).children('.pathValue').text() }), dataType: "json", success: function (data) { alert('Success = '+data.d); for (var i = 0; i < data.d.length; i++) { $('<div class="MainFolder"><span class="glyphicon glyphicon-folder-close folder-icon"></span>' + data.d[i] + '<span style="visibility: visible;" class="pathValue">' + parentPath + data.d[i] + '/</span></div>').appendTo($self); } //$self = ""; //parentPath = ""; }, error: function (result) { alert("Error"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder1<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder1/</span> </div> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder2<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder2/</span> </div> 

On page load it will show the result like belo 在页面加载时,它将显示类似belo的结果

Folder1

~/MyDocuments/Folder1/             //File Path                              

Folder2

~/MyDocuments/Folder2/                

When User click on Folder1 , I will call ajax and pass the corresponding url and it will get the subfolder name 当用户单击Folder1时 ,我将调用ajax并传递相应的url,它将获得子文件夹名称

Folder1

~/MyDocuments/Folder1/             //File Path     

    SubFolder1
    ~/MyDocuments/Folder1/SubFolder1   
    SubFolder2
    ~/MyDocuments/Folder1/SubFolder1
    SubFolder3                      
    ~/MyDocuments/Folder1/SubFolder1   

Folder2

~/MyDocuments/Folder2/    

Until this, it is working fine. 在此之前,它工作正常。

When I select SubFolder1 or SubFolder2 or SubFolder3 , that click function is calling two times, And giving the same file name like below 当我选择SubFolder1SubFolder2SubFolder3 ,该click函数被调用两次,并给出如下相同的文件名

Folder1
    SubFolder1
    SubFolder2
    SubFolder3                      
    SubFolder1
    SubFolder2
    SubFolder3                      

Folder2

Update 更新资料

When I click on SubFolder1 , on my alert it is printing like below 当我单击SubFolder1 ,在我的警报下,它的打印如下

First alert: childrent = ~/MyDocuments/Folder1/SubFolder1/ 第一个警报:childrent =〜/ MyDocuments / Folder1 / SubFolder1 /

Second alert: childrent = ~/MyDocuments/Folder1/ 第二次警报:childrent =〜/ MyDocuments / Folder1 /

and then It is calling ajax url and calling two times with those two url's. 然后它调用ajax url,并用这两个url调用两次。 But my first url is correct . 但是我的第一个网址是正确的 I don't want to get the second url. 我不想获取第二个URL。

To show the difference clearly, the area that can be clicked set as a red square. 为了清楚显示差异,可以单击的区域设置为红色正方形。

This is a simple demo showing why AJAX triggered multi times. 这是一个简单的演示,显示了为什么AJAX多次触发。

 var fakeSubFolder = '<div class="MainFolder"><span class="glyphicon glyphicon-folder-close folder-icon"></span><span style="visibility: visible;" class="pathValue">' + 'SubFolder' + '/</span></div>' $(document).on('click', '.MainFolder', function() { var $self = $(this) $(fakeSubFolder).appendTo($self); }); 
 .MainFolder{ outline: 1px red solid; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder1<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder1/</span> </div> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder2<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder2/</span> </div> 

And here's a demo showing how to overcome. 这是演示如何克服的演示。

 var fakeSubFolder = '<div class="MainFolder"><span class="glyphicon glyphicon-folder-close folder-icon"></span><span style="visibility: visible;" class="pathValue">' + 'SubFolder' + '/</span></div>' $('body').on('click', '.pathValue', function() { var $self = $(this) var $parent = $self.parent() $(fakeSubFolder).appendTo($parent); }); 
 .pathValue{ outline: 1px red solid; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder1<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder1/</span> </div> <div class="MainFolder" style="padding-left: 20px; width: 200px;"> <span class="glyphicon glyphicon-folder-close folder-icon"></span>Folder2<br> <span style="visibility: visible;" class="pathValue">~/MyDocuments/Folder2/</span> </div> 

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

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