简体   繁体   English

尝试合并jquery.each函数的所有值

[英]Trying to combine all value from jquery.each function

Look at my code, I am trying to combine all value from the jquery.each to a string like this ok,good, and then passed to ajax value. 看我的代码,我试图将jquery.each所有值组合到这样的字符串中,好,然后传递给ajax值。 Appreciate. 欣赏。 or array('ok','good') acceptable also 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"> 

Try this: 尝试这个:

 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); 
 <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"> 

You can use $.map to go over all of the items and return the value for each element. 您可以使用$ .map遍历所有项目并返回每个元素的值。
There is an example of both Array and String values, you can use whichever works better for you. 有一个ArrayString值的示例,您可以使用对自己更合适的值。

 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"> 

Try this working snippet 试试这个工作片段

 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"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM