jQuery 将输入类型=文本更改为文本区域

发布于 2025-01-01 12:37:50 字数 854 浏览 1 评论 0原文

我有一个隐藏字段。在放置事件之后,它需要转换为“文本区域”。

这:

      excerpt = $(parent).find('#excerpt').attr('type', 'textarea');
      excerpt.val('textarea');

产生

属性无法更改

错误

此方法: 将元素类型从隐藏更改为输入

marker = $('<span />').insertBefore('#myInput');
$('#myInput').detach().attr('type', 'textarea').insertAfter(marker);
marker.remove();

使用“textarea”不执行任何操作' ,但仅适用于 'text'。添加:

.val('HERE')

到 :

$('#myInput').detach().attr('type', 'textarea').val('HERE').insertAfter(marker);

行确实会导致文本框的值发生变化,因此选择器正在工作并且正在插入 元素和正确删除。

这是一个无法克服的安全问题吗?或者有办法做到吗?

I have a hidden field . After a drop event it needs to be transformed to a 'textarea'.

This:

      excerpt = $(parent).find('#excerpt').attr('type', 'textarea');
      excerpt.val('textarea');

Produces the

property cannot be changed

error

This method :
Change element type from hidden to input

marker = $('<span />').insertBefore('#myInput');
$('#myInput').detach().attr('type', 'textarea').insertAfter(marker);
marker.remove();

Does nothing using 'textarea' , but works for just 'text'.Adding:

.val('HERE')

To the :

$('#myInput').detach().attr('type', 'textarea').val('HERE').insertAfter(marker);

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈甜 2025-01-08 12:37:50

由于 textareainput 是完全不同的元素,因此更容易创建一个新的 textarea 并删除 input 。试试这个:

$input = $("#myHiddenInput")
$textarea = $("<textarea></textarea>").attr({
    id: $input.prop('id'),
    name: $input.prop('name'),
    value: $input.val()
});
$input.after($textarea).remove();

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();
你是年少的欢喜 2025-01-08 12:37:50

您始终可以删除要更改的元素并添加新元素...

You can always remove the element you want to change and add a new one...

夜未央樱花落 2025-01-08 12:37:50

您面临的问题是 TextArea 是一个元素而不是属性。

您应该删除以前的元素并替换为同名的新文本区域:

 var myoldInput = $('#myInput');  
 var value = myoldInput.val();
 $("<textarea id='myInput'></textarea>").before(myoldInput).val(value);
 myoldInput.remove();

The problem you face is that TextArea is a an element not an attribute.

You should look to remove the previous element and replace with a new text area of the same name:

 var myoldInput = $('#myInput');  
 var value = myoldInput.val();
 $("<textarea id='myInput'></textarea>").before(myoldInput).val(value);
 myoldInput.remove();
桃气十足 2025-01-08 12:37:50
$("<textarea>value</textarea").insertAfter($(parent).find('#excerpt'));
$(parent).find('input#excerpt').remove()
$("<textarea>value</textarea").insertAfter($(parent).find('#excerpt'));
$(parent).find('input#excerpt').remove()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文