Javascript 用另一个动态创建的输入替换输入
我想将现有的文件输入字段完全替换为文本输入字段,所有属性都保持不变,如Javascript 用另一个动态创建的输入替换输入,javascript,jquery,html,internet-explorer,Javascript,Jquery,Html,Internet Explorer,我想将现有的文件输入字段完全替换为文本输入字段,所有属性都保持不变,如id,name,等等。如何使用jQuery实现这一点 前奏曲: 我有一个,用于基于AJAX的上传。(是的,对于IE,我使用IFRAMEs提交表格。) 上传完成后,我进入我的成功回调函数,在那里我试图更改原始的长寿jQuery var fileInput = $('input[type="file"]'); var textInput = $('<input type="text"/>');
id
,name
,等等。如何使用jQuery实现这一点
前奏曲:
我有一个
,用于基于AJAX的上传。(是的,对于IE,我使用IFRAME
s提交表格。)
上传完成后,我进入我的成功回调函数,在那里我试图更改原始的长寿jQuery
var fileInput = $('input[type="file"]');
var textInput = $('<input type="text"/>');
//can copy properties here
textInput.value = 'response code';
textInput.className = fileInput.className;
textInput.insertBefore(fileInput);
textInput.remove();
var fileInput=$('input[type=“file”]”);
var textInput=$('');
//您可以在此处复制属性
textInput.value='响应代码';
textInput.className=fileInput.className;
textInput.insertBefore(fileInput);
textInput.remove();
jQuery万岁
var fileInput = $('input[type="file"]');
var textInput = $('<input type="text"/>');
//can copy properties here
textInput.value = 'response code';
textInput.className = fileInput.className;
textInput.insertBefore(fileInput);
textInput.remove();
var fileInput=$('input[type=“file”]”);
var textInput=$('');
//您可以在此处复制属性
textInput.value='响应代码';
textInput.className=fileInput.className;
textInput.insertBefore(fileInput);
textInput.remove();
IE不允许这样做,但解决方法可以是:
$('body').find('input:file').each(function() {
$("<input type='text' />").attr({value: 'response code' }).insertBefore(this);
}).remove();
$('body').find('input:file').each(function(){
$(“”).attr({value:'response code'}).insertBefore(this);
}).remove();
IE的可能副本不允许这样做,但解决方法可以是:
$('body').find('input:file').each(function() {
$("<input type='text' />").attr({value: 'response code' }).insertBefore(this);
}).remove();
$('body').find('input:file').each(function(){
$(“”).attr({value:'response code'}).insertBefore(this);
}).remove();
可能重复的我认为您可以通过放置两个输入来实现这一点
<input type="file" name="somename1" id="someid1"/>
<input type="text" name="somename1-after" id="someid1-after" style="display:none"/>
在您的请求回调中,您可以删除type=“file”
输入字段,去掉“-after”
后缀并显示type=“text”
输入字段。我认为您可以通过放置两个输入来实现这一点
<input type="file" name="somename1" id="someid1"/>
<input type="text" name="somename1-after" id="someid1-after" style="display:none"/>
var input = $("input[type=file]");
var inputHTML = input.clone().wrapAll("<div>").parent().html();
var newinputHTML = inputHTML.replace(/type="file"/, 'type="text"');
input.replaceWith(newinputHTML);
在您的请求回调中,您可以删除type=“file”
输入字段,去掉“-after”
后缀并显示type=“text”
input字段。var input=$(“input[type=file]”);
var input = $("input[type=file]");
var inputHTML = input.clone().wrapAll("<div>").parent().html();
var newinputHTML = inputHTML.replace(/type="file"/, 'type="text"');
input.replaceWith(newinputHTML);
var inputtml=input.clone().wrapAll(“”.parent().html();
var newinputtml=inputtml.replace(/type=“file”/type=“text”);
input.replacetwith(newinputtml);
var-input=$(“input[type=file]”);
var inputtml=input.clone().wrapAll(“”.parent().html();
var newinputtml=inputtml.replace(/type=“file”/type=“text”);
input.replacetwith(newinputtml);
根据用户2259571的回答,我以一行较短的方式完成了以下工作:
$originalInput
.replaceWith( $originalInput[0].outerHTML.replace(/type="file"/, 'type="input"') );
根据user2259571的回答,我用一行较短的方式做了以下工作:
$originalInput
.replaceWith( $originalInput[0].outerHTML.replace(/type="file"/, 'type="input"') );
删除旧输入并插入新输入如何?如何用最少的代码完成?我是否需要在jQuery
中动态创建input
,并迭代原始输入的所有属性,将它们推到新的位置?如何删除旧输入并插入新输入?如何使用最少的代码?我是否需要在jQuery
中动态创建input
,并迭代原始输入的所有属性,将其推送到新的位置?如何将以前文件输入的所有属性复制到新的文本输入中?您可以使用attr
方法,或者只需简单的Javascript,如何将以前文件输入的所有属性复制到新的位置文本输入?您可以使用attr
方法,也可以只使用简单的Javascript。这很酷。我将其更改为:input.replaceWith(input[0].outerHTML.replace(/type=“file”/type=“input”)代码>outerHTML可能无法在Firefox上运行这很酷。我将其更改为:input.replaceWith(input[0].outerHTML.replace(/type=“file”/type=“input”)代码>outerHTML可能无法在Firefox上工作