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:
?
123456789 |
//获取每一个项目的待处理的案件的数目,传入那个项目的获取数目的url,和那个项目的id
function getNumber(url,id){
$.
get
(url,function(data){
$(
"\"#"
+id+
"\""
).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:
?
123456789 |
//获取每一个项目的待处理的案件的数目,传入那个项目的获取数目的url,和那个项目的id
function getNumber(url,id){
$.get(url,
function
(data){
$(id).append(
"("
+data.total+
")"
);
if
(data.total!=0){
$(id).css(
"background"
,
"#f99"
);
}
});
}
|
Call Mode:
?
123 |
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