繁体   English   中英

获取具有相同输入文件标签的元素名称

[英]Get Elements Name with the same Input File tag

我在jQuery代码中有问题。 我有两个名称不同的HTML输入文件代码。 如何在jQuery中获取第二个输入文件名

这是我的HTML代码:

<input type="file" name="photo_a"   />
<input type="file" name="photo_b" />

这是我的jQuery代码:

var nameElement = $('input[type=file]').attr('name');
alert(nameElement);

我如何获得名称photo_b ,我的jquery代码仅获得photo_a名称。

请帮我解决这个问题:)

使用jeq eq通过选择中的索引获取元素,选择提供两个输入, eq(1)通过索引选择第二个输入。 另外,根据要求,使用change事件侦听器,当您选择要上传的图像时,您将获得所选的文件按钮名称:

 $(document).ready(function(){ $('input[type=file]').change(function(){ alert($(this).attr("name")); }); $("#secFileName").html($('input[type=file]').eq(1).attr('name')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="photo_a" /> <input type="file" name="photo_b" /> <br /><br /> Second File Name <div id="secFileName"></div> 

为什么不简单地仅使用js函数呢?

document.getElementsByName("photo_b");

你必须做

 //it gets current changed file name function getName(thisObj){ alert($(thisObj).attr("name")); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="photo_a" onchange="getName(this)" /> <input type="file" name="photo_b" onchange="getName(this)" /> 

暂无
暂无

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

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