Saturday, 15 March 2014

Add attribute to selected item using jquery -


I have a lot of selection listboxes which use the selected -jquery plugin to set the attribute property of the selected list items to me is required.

  $ (". Selected-select"). Selected (). Change (function (evt) {var label = $ (this (Labels == "Global Group") {$ (this.options [this.selectedIndex]) Option (labeling) .attr ("Group", "Global Group" );} And if (label == "personal group") {$ (this option [this. Left index]). Attr ("group", "personal group");} other {$ (This.options [this .selectedIndex]) attr ("group", "person");}});  

label will return the correct value for the first time but for subsequent selections No. I believe that I currently need to set selected items. Then, in the code-back, I will use it to set the values ​​in different lists.

How do I set the currently selected item to set the attribute value?

Thank you.

Update

  & asp: list box id = "lstBoxTo" runat = "server" SelectionMode = "multiple" data- Placeholder = "select recipient") ... "multiple =" true "class =" selected-selection "& gt; & Lt; / ASP: ListBox & gt; This is the whole document. Function:  
  $ (document) .ready (function () (/) Create a group for recipient dropdown list $ (". Select the selected option [group = 'global group'] Wrap ("& lt; opt-group label = 'global group' & gt;"); $ (". (Grouping = 'personal group')"). Wrap ("and lieutenant; opt-group label = 'Personal group' & gt; "); $ (" select-chosen option [group = 'individuals'] "). WrapAll (" & lt; Optgroup label = 'Individuals' & gt; "); //" Chosen 'jquery plugin $ ("choosen-selection") Configure the list box by using ({Search_contains: true, no_results_text: "Sorry, no mail!", $ ('Chosen-container'). Css ('width', '600px'); $ ("selected ('Label'); (label); alert (label); if the option ('evt') {var label = $ (This.options [this.selectedIndex]) is closest ('optgroup'); (Label == "global group") {$ (this option [this.selectedIndex]). Attr ("group", "global group");} and if (label == "personal group") {$ (this .options [this.selectedIndex]). Attr ("group", "personal group"); } And {$ (this.options [this.selectedIndex]). Attr ("group", "person"); }}); });  

This should work (not tested)

 ("option selected") . ("Option selected"). Selected (). Change (function (evt) {$ ("Chosen-selection"). ("Options: Selected"). Each (function () {var label = $ (this) .closest ('optgroup') .prop (label '$' (This) .attr ("group", "global group" ("group", "personal group") {$ (this) .attr ("Group", "personal group");} and {$ (this) .attr ("group", "person");}}};});  
< / Html>

No comments:

Post a Comment