jQuery:如何序列化不是输入的表单内的数据?

时间:2012-03-11 16:05:26

标签: jquery serialization

使用serialize()我没有问题来序列化在表单的输入文本字段中插入的值,但是..如何向该序列化数据添加<span>标记内容也在里面那种形式?

1 个答案:

答案 0 :(得分:1)

我会将span内容添加到隐藏输入中的表单中,然后像往常一样序列化表单...

对于像这样的HTML

<form id="myForm"><input type="text" name="myInput" /><span id="mySpan">this should be captured</span></form>

在序列化表单之前运行此

$("#myForm span").each(function(i,v){
    $this = $(this)
    $("#myForm").append(
        $("<input type='hidden' />").attr({
            name:$this.attr('id'),
            value: $this.text()
        })
    )

})
相关问题