简体   繁体   English

转换textarea中的输入字段

[英]transform input field in textarea

I have a form. 我有一个表格。 Within this form, I have an input field. 在此表单中,我有一个input字段。 I want to change some input field in the text area. 我想在文本区域中更改一些input字段。 I receive this form from an external source, so I can't change the html. 我从外部来源收到此表格,因此无法更改html。 How make this in js/jquery? 如何在js / jquery中做到这一点?

This is an example: 这是一个例子:

<label for="MMERGE12" class="MMERGE12-label"><span class="MMERGE12-label">categorie trattate</span>
                                                                        <input id="yikes-easy-mc-form-1-MMERGE12" name="MMERGE12" placeholder="" class="yikes-easy-mc-text" type="text" value="">

                                    <!-- description -->
                                    <p class="form-field-description"><small></small></p>                                   
                                                                        </label>

SOLVED WITH THIS CODE : 用此代码解决:

var input    = document.getElementById('yikes-easy-mc-form-1-MMERGE12'),
    textarea = document.createElement('textarea');
textarea.id    = input.id;
textarea.cols  = 40;
textarea.rows  = 5;
textarea.value = input.value;
textarea.name = input.name;
input.parentNode.replaceChild(textarea, input);
    //Create a new textarea
    var textarea = jQuery("<textarea></textarea>");

    //Select the input 
    var input = jQuery("#yikes-easy-mc-form-1-MMERGE12");

    //Asign the value, id, name from the input to the textarea
    textarea.val(input.val());
    textarea.attr('id', input.attr('id'));
    textarea.attr('name', input.attr('name'));
    //You can copy any other attribute you like here

    //Finally do the replacement
    input.reaplceWith(textarea);

you will probably need to wrap this in ready wrapper jQuery(document).ready(function() { /* code here */ }) if you do it on load or if you load it with ajax just execute this code after the data has been attached to the DOM 如果您在加载时执行此操作,或者如果您使用ajax加载,则可能需要将其包装在准备好的jQuery(document).ready(function() { /* code here */ }) ,只需在数据包含后执行此代码即可已附加到DOM

Try this 尝试这个

$(document).ready(function () {
        var input = $("#yikes-easy-mc-form-1-MMERGE12");
        var textArea = $("<textarea></textarea>").attr({
            id: input.attr("id"),
            name: input.attr("name"),                    
            value: input.val()
        });
        // class is set separately because "class" is a reserved word ...
        textArea.attr("class", input.attr("class"));
        //insert textarea right after original input, and remove input
        input.after(textArea).remove();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM