js list selection

It is preferred to define an array, and then traverse the array to get the selected value when operating

function getSelect(userId) {

            // var userId = userCheckBox.value; 
            // mark delete or add 
            var isAdd = true ;
             // if already exists, delete 
            for ( var i = 0; i < this .UserIdArray.length; i++ ) {
                 // in the collection Whether there is a user id 
                if ( this .UserIdArray[i] == userId) {
                     // delete the user id in the collection 
                    this .UserIdArray = this .UserIdArray.splice(0, i).concat( this .UserIdArray.splice(i + 1, this .UserIdArray.length));
                     // Mark is deleted
                    isAdd = false ;

                    break;
                }
            }
            // If it is an add, then add to the array 
            if (isAdd) {

                this.UserIdArray.push(userId);
            }

How to operate

function Delete()
{
    if (this.UserIdArray.length < 1) {
                alert( "Please select the data to delete!" );
                 return  false ;
            }
            else  if (confirm("Are you sure you want to delete the selected data?" )) {
                 var str = "" ;
                 for ( var i = 0; i < this .UserIdArray.length; i++ ) {

                    str += this.UserIdArray[i];
                    if (i + 1 != this.UserIdArray.length) {
                        str += ",";
                    }
                }
                // str is the selected value

    }

}    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325201977&siteId=291194637