jQuery serialize() leaves out textarea

前端 未结 9 1919
醉梦人生
醉梦人生 2020-12-16 11:33

When I submit a form using jQuery\'s serialize() method, everything gets submitted except the textarea in the form. Is this a common issue? I can\'t figure it out. The form

相关标签:
9条回答
  • 2020-12-16 11:49

    It leaves out the textarea , unless you Remove 'form="new_note_form"' from your textarea element.

    I know it's against good practices, but, if you want to use jQuery's serialize function, you gotta remove this attribute from textarea element.

    0 讨论(0)
  • 2020-12-16 11:51

    I have the same experience. Submitting a form using $("#form_id").serialize() does not include the textarea fields. This behavior is consisent for the past 2 years in the only form that has textarea elements. Every now and then I re-examine form and code to conclude that it should work, but doesn't.

    My work-around is, unsurprisingly, to first move the content of the textareas into hidden text boxes and then serialize the form data.

    0 讨论(0)
  • 2020-12-16 11:52

    We ran into the same problem with a textarea not being serialized despite having the name attribute set, and noticed it depended on where in the form the textarea was placed. We had the luxury of moving the textarea to another location on the form to resolve the problem.

    0 讨论(0)
  • 2020-12-16 11:54

    No it doesn't.

    It works fine. http://jsfiddle.net/nuBkM/

    <form>
        <input name="foo" value="bar"/><br>
        <textarea name="something">lorem ipsum</textarea>
    </form>
    

    The JavaScript

    console.log($("form").serialize());
    // => foo=bar&something=lorem+ipsum 
    

    .serializeArray works too

    console.log($("form").serializeArray());
    // => [{name: "foo", value: "bar"}, {name: "something", value: "lorem ipsum"}] 
    
    0 讨论(0)
  • 2020-12-16 11:58

    If the textarea is controlled by an editor like tinyMCE, you may need to call tinyMCE.triggerSave(), as described in this answer.

    0 讨论(0)
  • 2020-12-16 12:05

    Works fine in the fiddle. http://jsfiddle.net/Ultimate/2Ey2A/ Testing with

    $('button').click(function(){
        alert($('form').serialize());
    });
    
    0 讨论(0)
提交回复
热议问题