查看我的代码,我试图将jquery.each
中的所有值组合成这样的字符串,好,好,然后传递给ajax值。欣赏。或array('ok','good')
也可以接受
var global = {
"44":["onset","frequency"],
"45":["onset"]
};
var $val = global[44];
jQuery.each( $val, function( key ,value) {
var value = $('#'+value).val();
});
var $combine = ;//not sure how to combine all value like this (ok,good), or array(ok,good) acceptable also
var data= {
action: 'check_first',
AjaxFrontNonce : ajax_csky.AjaxFrontNonce,
combine : $combine
}
<input type="hidden" id="onset" value="ok">
<input type="hidden" id="frequency" value="good">
答案 0 :(得分:1)
试试这个:
var global = {
"44":["onset","frequency"],
"45":["onset"]
};
var $val = global[44];
var values = [];
jQuery.each( $val, function( key ,value) {
values.push($('#'+value).val());
});
var $combine = '(' + values.join(',') + ')';
console.log($combine);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="onset" value="ok">
<input type="hidden" id="frequency" value="good">
&#13;
答案 1 :(得分:1)
您可以使用$ .map覆盖所有项目并返回每个元素的值
有一个Array
和String
值的示例,您可以使用哪个更适合您。
var global = {
"44":["onset","frequency"],
"45":["onset"]
};
var $val = global[44];
combinedAr = $.map($val, function(val) {
return $('#'+val).val();
});
combinedStr = combinedAr.join(",");
var data= {
action: 'check_first',
AjaxFrontNonce : ajax_csky.AjaxFrontNonce,
combine : combinedStr
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="onset" value="ok">
<input type="hidden" id="frequency" value="good">
答案 2 :(得分:1)
试试这个工作代码段
var global = {
"44": ["onset", "frequency"],
"45": ["onset"]
};
var $val = global[44];
var arr = [];
$.each($val, function(key, val) {
var value = $('#' + val).val();
arr.push(value);
});
var $combine = arr; //not sure how to combine all value like this (ok,good), or array(ok,good) acceptable also
console.log($combine);
var data = {
action: 'check_first',
//AjaxFrontNonce : ajax_csky.AjaxFrontNonce, not defiend
combine: $combine
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="onset" value="ok">
<input type="hidden" id="frequency" value="good">