javascript jQuery serialize() 忽略了 textarea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17709014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery serialize() leaves out textarea
提问by Amoeba
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 works except for just the textarea which stays undefined???
当我使用 jQuery 的 serialize() 方法提交表单时,除了表单中的 textarea 之外的所有内容都被提交。这是一个常见的问题吗?我想不通。除了保持未定义的 textarea 之外,该表单有效???
<textarea form="new_note_form" id="note_text" name="note_text" required="required"></textarea>
回答by Daniel Sokolowski
It does not work untilyou add nameattribute to the textarea.
在向 textarea添加属性之前它不起作用name。
<textarea id="sLifeStyle3Content" name="sLifeStyle3Content" placeholder="HTML is allowed">
<apex:outputText value="{!sLifeStyle3Content}" />
</textarea>
<textarea id="sLifeStyle3Content" name="sLifeStyle3Content" placeholder="HTML is allowed">
<apex:outputText value="{!sLifeStyle3Content}" />
</textarea>
回答by Thank you
No it doesn't.
不,它没有。
It works fine. http://jsfiddle.net/nuBkM/
它工作正常。http://jsfiddle.net/nuBkM/
<form>
<input name="foo" value="bar"/><br>
<textarea name="something">lorem ipsum</textarea>
</form>
The JavaScript
JavaScript
console.log($("form").serialize());
// => foo=bar&something=lorem+ipsum
.serializeArrayworks too
.serializeArray也能用
console.log($("form").serializeArray());
// => [{name: "foo", value: "bar"}, {name: "something", value: "lorem ipsum"}]
回答by dtharpe
Another work around for this is to turn the textarea value into a variable and pass that with the ajax call...
另一个解决方法是将 textarea 值转换为变量并通过 ajax 调用传递它......
var comment = $('.note_comment').val();
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');
}
});
回答by user3261392
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.
我有同样的经历。使用 $("#form_id").serialize() 提交表单不包括 textarea 字段。在过去 2 年中,这种行为在唯一具有 textarea 元素的表单中是一致的。我时不时地重新检查表单和代码,以得出结论它应该可以工作,但不能。
My work-around is, unsurprisingly, to first move the content of the textareas into hidden text boxes and then serialize the form data.
不出所料,我的解决方法是首先将 textarea 的内容移动到隐藏的文本框中,然后序列化表单数据。
回答by PaulBGD
Works fine in the fiddle. http://jsfiddle.net/Ultimate/2Ey2A/Testing with
在小提琴中工作正常。 http://jsfiddle.net/Ultimate/2Ey2A/测试
$('button').click(function(){
alert($('form').serialize());
});
回答by Chuck Snell
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.
我们遇到了同样的问题,尽管设置了 name 属性,但 textarea 没有被序列化,并注意到它取决于 textarea 在表单中的位置。我们有幸将 textarea 移动到表单上的另一个位置来解决问题。
回答by Ismael Sarmento
It leaves out the textarea, unless you Remove 'form="new_note_form"'from your textarea element.
它会忽略textarea,除非您从 textarea 元素中删除 'form="new_note_form"'。
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.
我知道这违反了良好的做法,但是,如果您想使用 jQuery 的序列化功能,您必须从 textarea 元素中删除此属性。
回答by grvsmth
If the textarea is controlled by an editor like tinyMCE, you may need to call tinyMCE.triggerSave(), as described in this answer.
如果 textarea 由像 tinyMCE 这样的编辑器控制,您可能需要调用tinyMCE.triggerSave(),如本答案中所述。
回答by MistyDawn
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.
这是我用来在表单中包含/排除每个元素的方法。即使某些元素只有 id 的定义而不是名称,此方法也使我们的旧表单可序列化。
$( '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.
然后我调用下面的函数来获取关于$( '.answer' ).change()事件、页面导航和事件的序列化数组$('form').submit()。这种方法对我可以辨别的页面性能没有明显的负载。
function storeFormData() {
var serializedData = $( ".answer" ).serializeArray();
var formDataObj = serializedData;
var formDataString = JSON.stringify(formDataObj);
localStorage.setItem(fso_id, formDataString);
return formDataString;
}

