javascript - 改变输入类型

标签 javascript jquery html

我想通过单击按钮更改输入的类型,但它不起作用。我试过这个:

$('.addPhoto').click(function(){
 $(this).siblings('.auto_image').prop("type", "file");
});

它根本没有做任何事情。我在谷歌上搜索并发现了这个: change type of input field with jQuery

它说可以用直接的 DOM 来完成,但是我如何选择具有 DOM 的同级类?

最佳答案

您可以一起替换所有元素,但我认为您不能只更改类型属性,请参阅我的示例。

$('.addPhoto').on("click",function(){
    $(this).off("click").replaceWith("<input type='file' class='addPhoto'/>");
});

$('.addPhoto').one("click",function(){
    $(this).replaceWith("<input type='file' class='addPhoto'/>");
});

http://jsfiddle.net/ECzP4/

这里是 replaceWith() 的 jQuery 文档 http://api.jquery.com/replaceWith/

关于javascript - 改变输入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254811/

相关文章:

php - AJAX 调用如何与 TWIG 一起工作

javascript - 我的浏览器需要 JavaScript 才能运行

javascript - React 函数与模板重新渲染

javascript - 如何更改原型(prototype)值以覆盖对象值?是否可以?

javascript - 如何更新 meteor.js 中的 Mongodb 集合?

html - Thymeleaf - 使用多个片段进行布局的 div 类的正确顺序是什么?

c# - 一个好的 html 功能 richedit 替代 winforms?

javascript - 关注ajax调用后div中显示的成功消息

javascript - 服务器上的 JQuery 仅间歇性地使用 .appends()

javascript - Angular 4 Material 标题排序问题