how to switch between input type=“text” and textarea using jquery

血红的双手。 提交于 2019-12-23 23:25:53

问题


please help me on this code or another working code that switch between input type="text" and textarea using jquery.

$(function(){
$("a#change").toggle(function(){
  var input = document.getElementById('text'),
  textarea  = document.createElement('textarea');
  textarea.id    = input.id;
  textarea.cols  = 40;
  textarea.rows  = 5;
  textarea.value = input.value;
  input.parentNode.replaceChild(textarea,input);
  return false;
},function(){
  textarea.parentNode.replaceChild(input,textarea);
  return false;
});

});

<input type="text" name="text" id="text" /><a href="#" id="change">change</a>

回答1:


The following should work for you.

var textbox = $("#textbox");
var textarea = $("<textarea id='textarea'></textarea>");
$("#change").click(function () {
    //Check for textbox or textarea
    if ($("#textbox").length === 1) {
        //Copy value to textarea
        textarea.val(textbox.val());
        //Replace textbox with textarea
        textbox = textbox.replaceWith(textarea);
    } else {
        //Copy value to textbox
        textbox.val(textarea.val());
        //Replace textarea with textbox
        textarea = textarea.replaceWith(textbox);
    }
});

You can set the cols and rows in line two in you need to.

jsFiddle



来源:https://stackoverflow.com/questions/15934891/how-to-switch-between-input-type-text-and-textarea-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!