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