Checks whether the object is an empty object.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.