Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 提交时将HTML复制到文本区域_Javascript_Jquery_Html - Fatal编程技术网

Javascript 提交时将HTML复制到文本区域

Javascript 提交时将HTML复制到文本区域,javascript,jquery,html,Javascript,Jquery,Html,我有一个表单,其中包含多个输入块,这些输入块具有PHP指定的默认值,我希望将其中一个块的HTML标记传递到PHP脚本中。下面是我的表单的一个修改片段,以举例说明标记: <form action="/page/do_work/job_number" id="work_form"> <textarea style="display: none;" name="markup" id="markup"></textarea> <div id="b

我有一个表单,其中包含多个输入块,这些输入块具有PHP指定的默认值,我希望将其中一个块的HTML标记传递到PHP脚本中。下面是我的表单的一个修改片段,以举例说明标记:

<form action="/page/do_work/job_number" id="work_form">
    <textarea style="display: none;" name="markup" id="markup"></textarea>
    <div id="block_1">
        <table>
            <tr>
                <td><input type="text" value="123" /></td>
                <td><input type="text" value="123" /></td>
            </tr>
        </table>
    </div>
    <div id="block_2">
        <table>
            <tr>
                <td><input type="text" value="abc" /></td>
                <td><input type="text" value="abc" /></td>
            </tr>
        </table>
    </div>
</form>
问题是修改后的值没有复制到文本区域。例如,如果要将值从“abc”更改为“xyz”,则传递到textarea的标记仍然显示为“abc”。任何帮助都将不胜感激

编辑:使用
.html()
.val()
将标记添加到文本区域,但我想知道为什么插入文本区域的标记中没有反映输入值的更改。进一步检查后,更改输入字段的值,然后在Firebug中检查它们,显示保留默认值。我是否需要以某种方式更新DOM

编辑2:正在设置
标记
变量,但我对输入字段所做的更改不会反映在插入文本区域的标记中。

尝试

$('#markup', '#work_form').val( markup );
还要加入一个console.log(markup)以确保markup变量设置正确。

试试

$('#markup', '#work_form').val( markup );

另外,还要加入一个console.log(markup)以确保标记变量设置正确。

对于文本区域,您需要更改其的“值”而不是'innerhtml',而这正是.html所做的

$('#markup').val(markup)

试试这个。

对于文本区域,您需要更改它的'value'而不是它的'innerhtml',这就是.html所做的

$('#markup').val(markup)

试试这个。

对输入字段的更改不会更改DOM,这是我应该做的。要更改DOM,我编辑了
.submit()
函数,如下所示:

$('#work_form').submit(function(){
    // Update the DOM
    var block_1 = $('#block_1', '#work_form');
    block_1.find('input').each(function(i,e){
        el = $(e);
        el.attr('value', el.val());
    });
    // Snatch the markup
    var markup = block_1.html(); 
    // Place it into the textarea
    $('#markup', '#work_form').html( markup );
    // Move on
    return true;
});
感谢@PherricOxide为我指出这个问题:


对输入字段的更改不会更改DOM,这是我应该做的。要更改DOM,我编辑了
.submit()
函数,如下所示:

$('#work_form').submit(function(){
    // Update the DOM
    var block_1 = $('#block_1', '#work_form');
    block_1.find('input').each(function(i,e){
        el = $(e);
        el.attr('value', el.val());
    });
    // Snatch the markup
    var markup = block_1.html(); 
    // Place it into the textarea
    $('#markup', '#work_form').html( markup );
    // Move on
    return true;
});
感谢@PherricOxide为我指出这个问题:

签出此链接:签出此链接: