jQuery 选择器问题
jQuery 新手。
我有以下 html:
<form enctype="multipart/form-data" action="/upload/" method="post">
<div class="field">
<label for="id_profile_pic">Profile Pic</label>
<input type="file" name="profile_pic" id="id_profile_pic" />
</div>
<div>
<span>Profile Picture</span>
<img src="/site_media/profile_pic/4e17c792-3c34-4556-8c67-135bc931eb5a.jpg"/>
<input type="button" value="Upload" id="upload_button" />
</div>
</form>
我需要以下函数中选择器的帮助,
$('#upload_button').click(function() {
//how can I select the file input element (ie. #id_profile_pic) here?
});
谢谢。
New to jQuery.
I have the following html:
<form enctype="multipart/form-data" action="/upload/" method="post">
<div class="field">
<label for="id_profile_pic">Profile Pic</label>
<input type="file" name="profile_pic" id="id_profile_pic" />
</div>
<div>
<span>Profile Picture</span>
<img src="/site_media/profile_pic/4e17c792-3c34-4556-8c67-135bc931eb5a.jpg"/>
<input type="button" value="Upload" id="upload_button" />
</div>
</form>
And I need help with the selector in the following function
$('#upload_button').click(function() {
//how can I select the file input element (ie. #id_profile_pic) here?
});
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以像在初始选择器中定位第一个元素一样定位该元素:
您还打算对此做更多的事情吗?
You could target that element just as you targeted the first in your initial selector:
Was there something more you were intending to do with this?
为什么需要选择该元素?您可以使用
$('#id_profile_pic')
来完成此操作,但您不应该只提交表单以便实际上传吗?Why do you need to select that element? You would do it with
$('#id_profile_pic')
but shouldn't you just be submitting the form so it actually uploads?如果你想选择表单中的所有图片,你可以使用:
if you want to select all the pictures in the form you can use:
您应该以完全相同的方式执行此操作:使用哈希值 (
#
):You should do it the exact same way: with a hash (
#
):这也能做到这一点:
这将找到正在发布的表单中的文件控件,无论标记结构如何(并且不需要知道控件 ID)。
TMTOWTDI,正如 Perl 僧侣所说。
This will do it too:
This will find the file control(s) in the form being posted, regardless of markup structure (and without needed to know the control ID).
TMTOWTDI, as the perl monks say.