简体   繁体   English

形成如何从范围获取div元素

[英]Form how to get div element from span

I am having form for uploading picture, which contains many divs. 我有上传图片的表格,其中包含许多div。 I want to get the name of that picture which is inside div containing span. 我想获取包含span的div内的那张图片的名称。

<form class="dropzone dz-clickable dz-started" action="form_upload.html">
<div class="dz-preview dz-processing dz-error dz-complete dz-image-preview">
<div class="dz-image">
//skipped as it is showing complete image 
</div>
<div class="dz-details">
   <div class="dz-size">
      80kb
   </div>
   <div class="dz-filename">
     <span data-dz-name="">aust_ews.PNG</span>
   </div>
 </div>

</div>

</form>

I'm trying to get the aust_ews.PNG but failed to get. 我正在尝试获取aust_ews.PNG,但无法获取。

Here is my tries: 这是我的尝试:

var e =$(".dz-filename").val();//failed

var e =$("#form dz-filename").val();//failed

.val() fetches the value (think of input fields), but you need the contents between the tags - you need .html() . .val()获取值(考虑输入字段),但是您需要标记之间的内容-您需要.html()
Apart from that, you need to fetch the contents of the inner span , not the outer div - otherwise, the result would be <span data-dz-name="">aust_ews.PNG</span> . 除此之外,您还需要获取内部span而不是外部div -否则,结果将为<span data-dz-name="">aust_ews.PNG</span>

In code: 在代码中:

var e = $('.dz-filename span').html();

If it still doesn't work, verify that you have the jQuery library loaded. 如果仍然无法使用,请验证您是否已加载jQuery库。 You can check in the console by simply typing in $ and pressing enter. 您可以通过简单地输入$并按Enter来检入控制台。 It should state something like "function". 它应该声明类似“功能”的内容。

It's good practice to execute things like that inside ready . ready执行类似的事情是一个好习惯。 Just to be sure it'll work in the way you're expecting: 只是为了确保它能以您期望的方式工作:

$(document).ready(function()
{
  var e = $('.dz-filename span').html();
});

About ready see more on jquery.com manual . 关于ready更多信息,请参见jquery.com手册

Instead of 代替

$(".dz-filename").val();

try 尝试

$(".dz-filename").text();

As what ever html or text present inside the div is not the value, it is the content of that div. 由于div中存在的html或文本都不是值,因此它是该div的内容。 And you can get it by using html() or text(). 您可以使用html()或text()来获取它。

val() works only with input elements like text, select, radio, checkbox as it searches for the value attribute and return it. val()仅适用于输入元素,例如文本,选择,单选,复选框,因为它搜索value属性并返回它。

Working Fiddle 工作小提琴

Please check this. 请检查一下。 It's working here. 它在这里工作。 I have checked with the alert. 我已经检查了警报。

You just need to pass your code inside $(document).ready(function(){} function. 您只需要在$(document).ready(function(){}函数中传递代码。

 $(document).ready(function(){ var e = $(".dz-size").html(); alert(e) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <form class="dropzone dz-clickable dz-started" action="form_upload.html"> <div class="dz-preview dz-processing dz-error dz-complete dz-image-preview"> <div class="dz-image"> //skipped as it is showing complete image </div> <div class="dz-details"> <div class="dz-size"> 80kb </div> <div class="dz-filename"> <span data-dz-name="">aust_ews.PNG</span> </div> </div> </div> </form> 

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

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