我想通过单击按钮更改输入的类型,但它不起作用。我试过这个:
$('.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'/>");
});
这里是 replaceWith()
的 jQuery 文档 http://api.jquery.com/replaceWith/
关于javascript - 改变输入类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254811/