我正在尝试序列化并发布所有可能来自
答案 0 :(得分:0)
尝试使用$('#frm1').serialize();
var data = $('#frm1').serialize();
$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
console.log(response);
}
});
答案 1 :(得分:0)
没有表单元素也可以,
克隆指定的id元素,可以是其div
或其他任何值:
$('#myDiv').clone()
附加到form
元素中:
$('<form>').append($('#myDiv').clone()).serialize();
下面是工作示例,几乎包括了元素的类型:
function getSerialize(id){
let element = $("#"+id);
// if you are using form element
if( element.prop("tagName") === "FORM" ){
var data = element.serialize();
}
// if your are using another elements
else{
var myInputs = element.clone();
// this is the bug getting select box selected value when clone, so we have selected value in clone using this hack.
element.find('select').each(function(i) {
myInputs.find('select').eq(i).val($(this).val())
})
var data = $('<form>').append(myInputs).serialize();
}
// you can return 'data' variable from here.
console.log(data);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Div Serialize</h1>
<div id="myDiv">
<input type="text" name="name" placeholder="Name"><br><br>
<select name="age" placeholder="Age">
<option value="">Age</option>
<option value="18+">18+</option>
</select><br><br>
<textarea name="about" placeholder="About"></textarea><br><br>
<button onClick="getSerialize('myDiv')">Get Serialize</button>
</div>
<h1>Form Serialize</h1>
<form id="myDivForm" action="" onSubmit="return false;">
<input type="text" name="name" placeholder="Name"><br><br>
<select name="age" placeholder="Age">
<option value="">Age</option>
<option value="18+">18+</option>
</select><br><br>
<textarea name="about" placeholder="About"></textarea><br><br>
<button onClick="getSerialize('myDivForm')">Get Serialize Form</button>
</form>
希望获得帮助。