Today, on a data modification page, there is a dropdownlist control bound to an important attribute in the object. I want the user to prompt the user when modifying the control value, he modified the value of the control. Therefore, the following statement is added to the drop-down control.
CopyCode The Code is as follows: ddlmaterial. Attributes. Add ("onchange", "alert ('note: You have modified the item type! ')");
It seems good. Not only does it have a prompt effect, but you do not need to refresh the page. However, when I open the page and perform a test, I find the problem. When I change the value of the drop-down control back to the original value, a prompt is displayed, this will clearly give users an inexplicable feeling. What should I do? Use Ajax for server-side verification? This idea is too evil. I decided to try to implement it on the client using Js.
ImplementedAlgorithmFirst, I will search for the method to execute JS immediately after the page is loaded to obtain the initial value after the data is bound to the drop-down control. Google has achieved the following results:Copy codeThe Code is as follows: window. onload = function (){
VaR DDL = Document. getelementbyid ('ddlmaterial ');
VaR first = DDL. Options [DDL. selectedindex]. text;
}
My understanding is as follows:
Row 1st: After a page is loaded, the function () is executed. The function is a keyword. This is a strange usage.
Row 2nd: Get the control object through the control ID. This VaR is somewhat like an object.
Row 3rd: obtains the value of the currently selected row of the control.
Now, modify the code to meet my requirements.
// Background code
Ddlmaterial. Attributes. Add ("onchange", "changealert ()");
Front endCopy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
VaR first;
VaR DDL;
Window. onload = function (){
DDL = Document. getelementbyid ('ddlmaterial ');
First = DDL. Options [DDL. selectedindex]. text;
}
Function changealert (){
VaR sencond = DDL. Options [DDL. selectedindex]. text;
If (sencond! = First ){
Alert ("NOTE: You have modified the item type! ");
}
}
</SCRIPT>
To sum up, I suddenly found that there was a smart prompt when vs2008 was writing JS Code. (This is really great. The Gospel of cainiao, where does JS Code seem to be executed, I won't tell you if an error occurs.) It seems that I have already timed out ....