【问题标题】:How to get form html() via jQuery including <form></form> tag?如何通过包含 <form></form> 标签的 jQuery 获取表单 html()?
【发布时间】:2016-08-22 09:27:05
【问题描述】:

我想将我的 form html 内容显示到允许用户通过用户界面编辑表单元素的文本区域中。这是我的表格,

<form name="myform" action="abc.php">
    <input type="radio" name="radio1" value="1" checked>
    <input type="radio" name="radio2" value="2"><br><br>
    <input type="text" name="input" value="My Name" />
</form>

我尝试了下面的代码,但它只显示表单元素(不包括表单标签)。

<script>
$(document).ready(function() 
{
    $('a').on('click', function() { 
        $("input,select,textarea").each(function() {
           if($(this).is("[type='checkbox']") || $(this).is("[type='checkbox']")) {
             $(this).attr("checked", $(this).attr("checked"));
           }
           else {
              $(this).attr("value", $(this).val()); 
           }
        });
        alert($('form').html());
    });
});
</script>

上面的代码会提示如下输出,

<input type="radio" name="radio1" value="1" checked>
<input type="radio" name="radio2" value="2"><br><br>
<input type="text" name="input" value="My Name" />

但我需要如下输出,

<form name="myform" action="abc.php">
    <input type="radio" name="radio1" value="1" checked>
    <input type="radio" name="radio2" value="2"><br><br>
    <input type="text" name="input" value="My Name" />
</form>

【问题讨论】:

    标签: html forms tags


    【解决方案1】:

    希望您可以将这些功能用于$("form").clone().appendTo("body"); 表单,否则使用

    <div id='formdiv1'>
    <form name="myform" action="abc.php">
    <input type="radio" name="radio1" value="1" checked>
    <input type="radio" name="radio2" value="2"><br><br>
    <input type="text" name="input" value="My Name" />
    </form>
    </div>
    
    alert($('#formdiv1').html());
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2016-08-03
      • 2020-05-06
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多