jQuery将输入类型=文本更改为textarea [英] jQuery change input type=text to textarea
问题描述
我有一个隐藏的领域。发生拖放事件后,需要将其转换为文本区域。
I have a hidden field . After a drop event it needs to be transformed to a 'textarea'.
此:
excerpt = $(parent).find('#excerpt').attr('type', 'textarea');
excerpt.val('textarea');
产生
无法更改属性
property cannot be changed
错误
此方法:
将元素类型从隐藏更改为输入
marker = $('<span />').insertBefore('#myInput');
$('#myInput').detach().attr('type', 'textarea').insertAfter(marker);
marker.remove();
不使用'textarea'进行任何操作,仅适用于'text'。添加:
Does nothing using 'textarea' , but works for just 'text'.Adding:
.val('HERE')
到:
$('#myInput').detach().attr('type', 'textarea').val('HERE').insertAfter(marker);
行的确会导致文本框的值发生变化,因此选择器正在工作,而< span>
元素已正确插入和删除。
line does result in the value of the the text box changing , so the selector is working and the <span>
element is being inserted and removed correctly.
这是无法解决的安全问题吗?还是有办法做到这一点?
Is this an insurmountable security issue? Or is there a way of doing it?
推荐答案
因为 textarea
与<$ c完全不同$ c> input ,只需创建一个新的 textarea
并删除 input
会更容易。尝试以下操作:
Because a textarea
is a completely different element to input
it's easier to simply create a new textarea
and remove the input
. Try this:
$input = $("#myHiddenInput")
$textarea = $("<textarea></textarea>").attr({
id: $input.prop('id'),
name: $input.prop('name'),
value: $input.val()
});
$input.after($textarea).remove();
这篇关于jQuery将输入类型=文本更改为textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!