We sometimes need to clear the values in the options set in the select control,
I found it online, most of which are
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = 0; I <obj. Options. length; I ++)
OBJ. Options [I] = NULL;
However, this method does not seem to be able to complete the clearing operation.
I tested it several times. If I had the numbers from 0 to 8, after I ran a cleanup operation, I found that 1, 3, 5, and 7 still exist. If I click "clear" again, only 3, 7 exists. Reduce by half each time.
At first, I couldn't figure out why it was like this. Later I thought it might be like this:
Because obj. Options. length is decreased along with the user's obj. Options [I] = NULL; operation.
So when the fourth row is deleted, I = 4 and obj. Options. length are also equal to 4, so the loop is exited, so that only half is deleted.
Therefore, the correct method is to delete from the back.
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = obj. Options. Length-1; I> = 0; I --)
OBJ. Options [I] = NULL;
In this way, you can clear
Another method should be to replace obj. Options. length in the for loop with a variable and assign a value to this variable before the loop.
Javascript is really inconvenient. In. net, the clear method of the Set object is usually called, but it is so troublesome here.
We sometimes need to clear the values in the options set in the select control,
I found it online, most of which are
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = 0; I <obj. Options. length; I ++)
OBJ. Options [I] = NULL;
However, this method does not seem to be able to complete the clearing operation.
I tested it several times. If I had the numbers from 0 to 8, after I ran a cleanup operation, I found that 1, 3, 5, and 7 still exist. If I click "clear" again, only 3, 7 exists. Reduce by half each time.
At first, I couldn't figure out why it was like this. Later I thought it might be like this:
Because obj. Options. length is decreased along with the user's obj. Options [I] = NULL; operation.
So when the fourth row is deleted, I = 4 and obj. Options. length are also equal to 4, so the loop is exited, so that only half is deleted.
Therefore, the correct method is to delete from the back.
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = obj. Options. Length-1; I> = 0; I --)
OBJ. Options [I] = NULL;
In this way, you can clear
Another method should be to replace obj. Options. length in the for loop with a variable and assign a value to this variable before the loop.
Javascript is really inconvenient. In. net, the clear method of the Set object is usually called, but it is so troublesome here.