jQuery serialize() leaves out textarea

前端 未结 9 1920
醉梦人生
醉梦人生 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 12:07

    It does not work until you add name attribute to the textarea.

    <textarea id="sLifeStyle3Content" name="sLifeStyle3Content" placeholder="HTML is allowed">
      <apex:outputText value="{!sLifeStyle3Content}" />
    </textarea>
    
    0 讨论(0)
  • Another work around for this is to turn the textarea value into a variable and pass that with the ajax call...

    var comment = $('.note_comment').val();

               $.ajax({
                   type: "POST",
                   url: '/approot/rewrite.cfm/app.people/insertNote?format=json&Comment=' + comment,
                   data: $("form[name='add_note_form']").serializeArray(),
                   success: function(data)
                   {
                  alert('success');         
                   }
                 });
    
    0 讨论(0)
  • 2020-12-16 12:11

    This is what I use to include/exclude each of the elements as I need them from the form. This method also makes our older forms serializable even though some of the elements only have id's defined and not names.

    $( 'textarea' ).each( function() { 
      $(this).attr( 'type', 'textarea' ); 
    });
    
    $( 'input:text:not( ".excluded" ), input:checkbox, input:radio, textarea' ).each( function() {
    
      if (!$(this).hasClass( 'answer' )) { 
        $(this).addClass( 'answer' ); 
      }
    
      if ( !$(this).attr( "name" ) && $(this).attr( 'id' ) ) { 
        $(this).attr( "name", $(this).attr("id") ); 
      }
    
    });
    

    Then I call the function below to get my serialized array on the $( '.answer' ).change() event, on page navigation and on the $('form').submit() event. This method puts no noticeable load on the page performance that I can discern.

    function storeFormData() {
      var serializedData = $( ".answer" ).serializeArray();
      var formDataObj = serializedData;
      var formDataString = JSON.stringify(formDataObj);
      localStorage.setItem(fso_id, formDataString);
      return formDataString;
    }
    
    0 讨论(0)
提交回复
热议问题