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.
Copy codeThe 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.
The implementation algorithm is very simple. First, I will search for the JS method that will be executed immediately after the page is loaded to obtain the initial values 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 end
Copy 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 ....