jQuery 仅序列化 div 中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1829519/
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 to serialize only elements within a div
提问by BrokeMyLegBiking
I would like to get the same effect as jQuery.serialize()
but I would like to return only the child elments of a given div
.
我想获得相同的效果,jQuery.serialize()
但我只想返回给定div
.
Sample result :
示例结果:
single=Single2&multiple=Multiple&radio=radio1
回答by jitter
No problem. Just use the following. This will behave exactly like serializing a form but using a div's content instead.
没问题。只需使用以下内容。这将与序列化表单完全一样,但使用 div 的内容。
$('#divId :input').serialize();
Check https://jsbin.com/xabureladi/1for a demonstration (https://jsbin.com/xabureladi/1/editfor the code)
检查https://jsbin.com/xabureladi/1以获取演示(https://jsbin.com/xabureladi/1/edit获取代码)
回答by ThiagoPXP
You can improve the speed of your code if you restrict the items jQuery will look at.
如果您限制 jQuery 将查看的项目,则可以提高代码的速度。
Use the selector :inputinstead of *to achieve it.
使用选择器:input而不是*来实现它。
$('#divId :input').serialize()
This will make your code faster because the list of items is shorter.
这将使您的代码更快,因为项目列表更短。
回答by Zakaria Acharki
serialize
all the form-elements within a div
.
serialize
中的所有表单元素div
。
You could do that by targeting the div #target-div-id
inside your form
using :
您可以通过#target-div-id
在form
using 中定位 div 来做到这一点:
$('#target-div-id').find('select, textarea, input').serialize();
回答by Lukasz Frankowski
The function I use currently:
我目前使用的功能:
/**
* Serializes form or any other element with jQuery.serialize
* @param el
*/
serialize: function(el) {
var serialized = $(el).serialize();
if (!serialized) // not a form
serialized = $(el).
find('input[name],select[name],textarea[name]').serialize();
return serialized;
}
回答by Alberto Buschettu
Try also this:
也试试这个:
$('#divId').find('input').serialize()
$('#divId').find('input').serialize()
回答by Briganti
What about my solution:
我的解决方案呢:
function serializeDiv( $div, serialize_method )
{
// Accepts 'serialize', 'serializeArray'; Implicit 'serialize'
serialize_method = serialize_method || 'serialize';
// Unique selector for wrapper forms
var inner_wrapper_class = 'any_unique_class_for_wrapped_content';
// Wrap content with a form
$div.wrapInner( "<form class='"+inner_wrapper_class+"'></form>" );
// Serialize inputs
var result = $('.'+inner_wrapper_class, $div)[serialize_method]();
// Eliminate newly created form
$('.script_wrap_inner_div_form', $div).contents().unwrap();
// Return result
return result;
}
/* USE: */
// For: $('#div').serialize()
serializeDiv($('#div')); /* or */ serializeDiv($('#div'), 'serialize');
// For: $('#div').serializeArray()
serializeDiv($('#div'), 'serializeArray');
function serializeDiv( $div, serialize_method )
{
// Accepts 'serialize', 'serializeArray'; Implicit 'serialize'
serialize_method = serialize_method || 'serialize';
// Unique selector for wrapper forms
var inner_wrapper_class = 'any_unique_class_for_wrapped_content';
// Wrap content with a form
$div.wrapInner( "<form class='"+inner_wrapper_class+"'></form>" );
// Serialize inputs
var result = $('.'+inner_wrapper_class, $div)[serialize_method]();
// Eliminate newly created form
$('.script_wrap_inner_div_form', $div).contents().unwrap();
// Return result
return result;
}
/* USE: */
var r = serializeDiv($('#div')); /* or serializeDiv($('#div'), 'serialize'); */
console.log("For: $('#div').serialize()");
console.log(r);
var r = serializeDiv($('#div'), 'serializeArray');
console.log("For: $('#div').serializeArray()");
console.log(r);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div">
<input name="input1" value="input1_value">
<textarea name="textarea1">textarea_value</textarea>
</div>
回答by Yogesh Mistry
If those elements have a common class name, one may also use this:
如果这些元素有一个共同的类名,你也可以使用这个:
$('#your_div .your_classname').serialize()
This way you can avoid selection of buttons, which will be selected using the jQuery selector :input
. Though this can also be avoided by using $('#your_div :input:not(:button)').serialize();
通过这种方式,您可以避免选择按钮,这些按钮将使用 jQuery 选择器进行选择:input
。虽然这也可以通过使用来避免$('#your_div :input:not(:button)').serialize();
回答by jefissu
$('#divId > input, #divId > select, #divId > textarea').serialize();