JQUERY-based multi-level linkage code _ jquery

Source: Internet
Author: User
The code is implemented based on JQUERY's multi-level linkage. If you need more code, you can refer to it. In addition, you can try jquery. select. more. js.

The Code is as follows:


(Function ($ ){
$. Fn. doselectmore = function (settings ){
Var dfop = {
Namekey: "name ",
Pnamekey: "name ",
Idkey: "id ",
Selectname: "sel ",
Method: "POST ",
Datatype: "json ",
Param :{},
Pval: null,
Chckval: null,
Chckvalarry: null,
Pname: false,
Nname: false,
Vl: 0,
Url: false,
Data: false
};
$. Extend (dfop, settings );
// Alert (eobj (dfop ));
Var me = $ (this );
If (! Dfop. nname ){
Dfop. nname = (dfop. selectname + (dfop. vl + 1 ));
}
If ((! Dfop. pname) & dfop. vl> 0 ){
Dfop. pname = dfop. selectname + (dfop. vl-1 );
}
If (! Dfop. data ){
If (dfop. url ){
Var param = {};
$. Ajax ({
Type: dfop. method,
Url: dfop. url,
Data: dfop. param,
DataType: dfop. datatype,
Success: function (data ){
Dfop. data = data;
Selectmorebuilder (me, dfop );
},
Error :( function (request, status, err ){
Var errText = request. responseText;
Var ErrMessage = "+ request. status +" error message ,";
ErrMessage + = "error message:" + request. statusText + "\ n" + errText. substring (errText. indexOf ("

")+5,errText.indexOf("
"));
Alert (ErrMessage );
})
});
}
} Else {
Selectmorebuilder (me, dfop );
}
Function selectmorebuilder (thisme, df ){
If (df. chckval! = Null & df. chckvalarry = null ){
Var pid = df. chckval;
Var k = 1;
Df. chckvalarry = new Array ();
Df. chckvalarry. push (pid );
While (k> 0 ){
K = 0;
$. Each (df. data, function (I, item ){
If (item [df. idkey] = pid & item [df. pnamekey]! = Df. pval ){
Pid = item [df. pnamekey];
Df. chckvalarry. unshift (pid );
K ++;
}
});
}
}
// Alert (eobj (df. chckvalarry ));
Var select = $ (" ");
Select. attr ({
Name: df. selectname + dfop. vl,
Id: df. selectname + dfop. vl,
Nname: df. nname,
Pname: df. pname,
Vl: df. vl
});
Var sp = null;
If (dfop. vl = 0 ){
Sp = $ ("

");
Thisme. after (sp). remove ();
Sp. append (" ");
Sp. append (select );
Sp. get (0). t = df;
} Else {
Thisme. removeselectmore ();
Sp = $ ("input [name =" + df. selectname + "]"). parent ();
Sp. append (select );
}
Select. empty ();
Var counti = 0;
$. Each (df. data, function (I, item ){
If (item [df. pnamekey] = df. pval ){
Select. append (" "+ Item [df. namekey] +"");
Counti ++;
}
});
If (counti = 0 ){
Select. remove ();
}
Else {
Select. change (function (){
Var nselect = $ ("#" + $ (this). attr ("nname "));
If (nselect. length = 0 ){
Nselect = $ (" ");
Sp. append (nselect );
}
Nselect. doselectmore ({
Namekey: df. namekey,
Pnamekey: df. pnamekey,
Idkey: df. idkey,
Selectname: df. selectname,
Param: df. param,
Pval: $ (this). val (),
Vl: df. vl + 1,
Chckvalarry: sp. get (0). t. chckvalarry,
Data: df. data
});
});
If (df. chckvalarry! = Null ){
If (df. chckvalarry. length> = df. vl)
Select. val (df. chckvalarry [df. vl]);
}
If (df. vl = 1 & df. chckvalarry! = Null & df. chckvalarry [1]! = Select. val ()){
Var ddf = sp. get (0). t;
Ddf. chckvalarry = null;
Ddf. chckval = null;
Sp. get (0). t = ddf;
}
Select. change ();
$ ("Input [type = hidden] [name =" + df. selectname + "]"). val ($ ("input [type = hidden] [name =" + df. selectname + "]"). getselectmoreval ());
}
}
};
$. Fn. getselectmoreval = function (){
Var me = $ (this );
If (me. size () = 0) return;
Var sp = me. parent ();
If (sp. size () = 0) return;
Var df = sp. get (0). t;
Var nselect = $ ("#" + df. selectname + df. vl );
Var v = null;
While (nselect. size ()> 0 ){
V = nselect. val ();
Nselect = $ ("#" + nselect. attr ("nname "));
}
Return v;
};
$. Fn. setselectmoreval = function (idv ){
Var me = $ (this );
If (me. size () = 0) return;
Var sp = me. parent ();
If (sp. size () = 0) return;
Var df = sp. get (0). t;
Df. chckval = idv;
If (idv! = Null ){
Var pid = df. chckval;
Var k = 1;
Df. chckvalarry = new Array ();
Df. chckvalarry. push (pid );
While (k> 0 ){
K = 0;
$. Each (df. data, function (I, item ){
If (item [df. idkey] = pid & item [df. pnamekey]! = Df. pval ){
Pid = item [df. pnamekey];
Df. chckvalarry. unshift (pid );
K ++;
}
});
}
}
Sp. get (0). t = df;
Var nselect = $ ("#" + df. selectname + df. vl );
Nselect. val (df. chckvalarry [0]);
Nselect. change ();
};
$. Fn. removeselectmore = function (){
If ($ (this). attr ("nname ")! = Null ){
$ ("#" + $ (This). attr ("nname"). removeselectmore ();
}
$ (This). remove ();
};
}) (JQuery );


Help.html

The Code is as follows:


Bytes



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.