-------------------------------------------------------------------------------------------------------
/**
* Explicitly check the empty checkbox (jquery code)
* @ Black eyed poet <www.chenwei.ws>
*/varobj= $(': CheckBox');
Obj.on ('Click',function() {othis= $( This); if(Othis.attr ('selected') == 'selected') {othis.removeattr ('selected'); }Else{othis.attr ({'selected':true}); }});
/**
* Note: If you click the button (not directly click on the checkbox) to trigger the event, first uncheck the visible tick, and then clear the explicit property check
*
* For example: $ (': CheckBox '). each (function () {
* IF ($ (this). attr (' selected ') = = ' selected ')
* {
* $ (this). attr (' checked ', false); Uncheck the Visible check box
* $ (this). Removeattr (' selected '); Uncheck an explicit property check box
* }
* });
*/
/** * Explicitly save checkboxes and hidden values in an array (jquery code) * @ Black eyed poet <www.chenwei.ws>*/varinfo = {"Discount": {"discount_id": [], "discount_lesson_id" : []}}; Infodiscountid= info[' discount ' [' discount_id '];infodislessonid= info[' discount ' [' discount_lesson_id '];$(' Input[name= ' discount_id '). On (' click ',function(){ varOthis = $ ( This); if(Othis.attr (' selected ') = = ' selected ') {othis.removeattr (' Selected '); $.each (Infodislessonid,function(i, n) {if(Infodislessonid[i] = = Othis.prev (' input '). Val ()) {Deleteinfodislessonid[i];//Uniqueness Delete J= i;//corresponds to the i in Discountid } }); DeleteInfodiscountid[j]; Console.log (info); }Else{othis.attr ({' Selected ':true}); Infodiscountid.push (Othis.val ()); //Array new elementsInfodislessonid.push (Othis.prev (' input '). Val ());//new element 2Console.log (info); }});
/* There is a problem:
* 1. Delete Infodislessonid[i], although the elements are in Infodiscountid and Infodislessonid respectively, but if they have the same value, they will be deleted at this time. This is not what you want to happen.
* 2. Use delete Infodiscountid[i] When this form of deletion, the original array length is unchanged, the index is still in, but the current infodiscountid[i] value becomes undefined.
*/
/**
* Solve the problem in Example 2, replace the array format stored value (jquery code)
* @ Black eyed poet <www.chenwei.ws>
*/
var info = {"Discount": []};
Infodiscountid = info[' discount '];
$ (' input[name= ' discount_id "]). On (' click ',function(){ varOthis = $ ( This); if(Othis.attr (' selected ') = = ' selected ') {othis.removeattr (' Selected '); $.each (Infodiscountid,function(i, n) {if(infodiscountid[i][' discount_id ') = = Othis.val () && infodiscountid[i][' discount_lesson_id '] = = Othis.prev (' Input). Val ()) {//To make a unique decision before deleting, refer to how to store it when addingInfodiscountid.splice (i, 1); Use splice to delete a single element instead of Delete}}); Console.log (info); }Else{othis.attr ({' Selected ':true}); Infodiscountid.push ({"DISCOUNT_ID": Othis.val ()}); Push adds a pair of array elements at the end of the array, discount_idvarindex = infodiscountid.length-1; Get index of new element infodiscountid[index][' discount_lesson_id ' = othis.prev (' input ')). Val (); Add a pair of array elements under this index, discount_lesson_id console.log (info); }});
/*
* Perfect solution to problems in Example 2
*/
Scenario: The above example is a solution to a specific value that needs to be associated with multiple table data.
Link:http://www.cnblogs.com/farwish/p/3964350.html
[Jq]jquery explicitly operates a checkbox and uses an array to store the scheme of associated values