How to add a variable ID to a jquery selector

Source: Internet
Author: User

Today in the Guangzhou Arbitration Commission system such a requirement, need to check the number of cases of each item when the page is loaded, there are many projects, I wrote this at the beginning:

The code is as follows:

$.get (Assignedcaseurl,function(data) {$ ("#waitingAllocationSecretary"). HTML ("Waiting for the minister to assign the secretary for handling cases (" +data.total+ ")"); if(data.total!=0){            $("#waitingAllocationSecretary"). CSS ("Background", "#f99"));        }    }); $.get (Collectedcaseurl,function(data) {$ ("#waitingRegisterCase"). HTML ("I received a pending filing (" +data.total+ ")); if(data.total!=0){            $("#waitingRegisterCase"). CSS ("Background", "#f99"));        }    }); $.get (Filedcaseurl,function(data) {$ ("#waitingOrganize"). HTML ("I've filed a case for the group (" +data.total+ ")"); if(data.total!=0){            $("#waitingOrganize"). CSS ("Background", "#F99")); }    });

Obviously, for a lot of projects, this simple repetition looks like a very bloated code, so I, like many people, intend to encapsulate these functions with a function, at first:

    // get the number of pending cases for each item, the number of URLs that passed in that item, and the ID    of that item function GetNumber (url,id) {    $.get (URL,function(data) {        $ ("#" +id). Append ("(" + data.total+ ")");         if (data.total!=0) {            $ ("#" +id). CSS ("Background", "#f99");        }    });

But the program error, and I changed to the wrong way:

Get the number of pending cases for each item, the number of URLs that passed into that item, and the Idfunction GetNumber (url,id) {$.get ("\" # "Url,function" ). Append ("(" +data.total+ ")"), if (data.total!=0) {    $ ("\" # "+id+" \ "). CSS (" Background "," #f99 ");}    });}

Obviously, this way is also wrong, how to put my id value into my JQ selector, the following look at the correct wording:

Get the number of pending cases for each item, the number of URLs that passed into that item, and the Idfunction GetNumber (url,id) {$.get (ID) for that project. Url,function ("( "+data.total+"), if (data.total!=0) {    $ (id). CSS ("Background", "#f99");}    });

Call Mode:

GetNumber (assignedcaseurl,waitingallocationsecretary);    GetNumber (collectedcaseurl,waitingregistercase);    GetNumber (filedcaseurl,waitingorganize);

Where ID is the ID of my page, I call this function in the page when the ID is not a string, but a JQ object, so we call this variable in the selector, we can directly pass the ID of this variable, this error I made two times, so remember

How to add a variable ID to a jquery selector

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.