Encountered a problem in the project
1. check whether a group of checkboxes are selected
2. if selected, return the corresponding name value.
3. If none is selected, a false value is returned.
Solution
VaR checklist_box = function (Param, output) {var $ Boolean, $ result ={}; if (PARAM) {If (typeof Param = 'string ') {$ Boolean = $ (PARAM ). prop ('checked ')? True: false; $ result ['name _ '+ Param] = $ (PARAM ). ATTR ('name')} else if (typeof Param = 'object') {$. each (Param, function (I, n) {if ($ (n ). prop ('checked') {$ Boolean = true; $ result ['name _ '+ N] = $ (n ). ATTR ('name') ;}}) // you need to determine whether the object is empty. // The first incompatible method is first written, finally, we found that if (JSON. stringify (result) ===' {} ') {$ Boolean = false; return false} // method 2, using the enumerated object method $ Boolean = checkobj (result) if ($ Boolean) {return result} else {return false }}} else {return false} return result}
The checkobj () method is as follows:
var checkObj = function(param){ if(typeof param !== ‘object’ || param === ‘null’){ return false } else{ var k =0; for (var i in param){ if(param.hasOwnProperty(n)){ k++ } } return !!k}}
Finished !!
After writing, I suddenly found myself stupid. I can use the selector in jquery to do the same thing.
var arr = $(':checkbox")var result = {}$.each(arr,function(i,n){ if($(this).prop("checked")){ result['name'+n] = $(this).attr('name') } return result})
Let's leave a lesson !!!