jQuery将隐藏的输入类型更改为textarea [英] jQuery change hidden input type to textarea

查看:114
本文介绍了jQuery将隐藏的输入类型更改为textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏的领域。在放弃事件之后,它需要转换为'textarea'。





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

生成


属性无法更改

错误

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

  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); 

行确实会导致文本框的值发生变化,所以选择器正在工作, code>< span> 元素正被插入并正确移除。

这是一个难以逾越的安全问题吗?或者有没有办法呢?因为 textarea 与<$ c是完全不同的元素 $ c> input 简单地创建一个新的 textarea 并移除 input 会更容易。试试这个:

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


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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆