Home  >  Article  >  Web Front-end  >  The sum of several attributes of a JS gadget cannot exceed a specific value._javascript tips

The sum of several attributes of a JS gadget cannot exceed a specific value._javascript tips

WBOY
WBOYOriginal
2016-05-16 18:45:301133browse

The most famous example is the assignment of attribute values ​​​​to new generals in Romance of the Three Kingdoms and Taige.
I have used javascript to create such a function, and by the way, hide the underline of the links in the page.
Everyone has a look Look, if you have any better optimization suggestions, feel free to mention them.
The following is the code:
The first is test.html:

Copy the code The code is as follows:




New Document











< td height="30" bordercolor="#FFFFFF" bgcolor="#D0E8FF" class="font1">
username

< td height="30" bordercolor="#FFFFFF" bgcolor="#D0E8FF" class="font1">








< ;/font>

< div align="center">
meili




< ;/font>





Also link.css:
Copy code The code is as follows:

.A {
text-transform: none;
text-decoration: none;
}
.a:hover {
text -decoration: underline
}

Finally util.js (this code is quite long):
Copy code The code is as follows:

var flag=false;
function add(formname){
if (formname.username.value == "") {
alert("Please enter a positive integer");
formname.username.focus();
return false;
}
flag = checkscope(formname);
if(flag==false){
return false;
}
if( parseInt(formname.username.value)>0){
formname.username.value =parseInt(formname.username.value) 1;
if(parseInt(formname.username. value) parseInt(formname.meili.value)>10){
alert("The sum of the entered integers must not be greater than 10");
formname.username.value =10-parseInt(formname.meili.value );
return false;
}else
return true;
}else{
alert("The entered integer must be greater than 0");
formname.username.value =1 ;
return false;
}
}
function subtration(formname){
if (formname.username.value == "") {
alert("Please enter a positive integer");
formname.username.focus();
return false;
}
flag = checkscope(formname);
if(flag==false){
return false;
}
if( parseInt(formname.username.value)>1){
formname.username.value =parseInt(formname.username.value)-1;
if(parseInt (formname.username.value) parseInt(formname.meili.value)>10){
alert("The sum of the entered integers must not be greater than 10");
formname.username.value =10-parseInt( formname.meili.value);
return false;
}else
return true;
}else if(parseInt(formname.username.value) ==1){
alert(" It is already the minimum value and cannot be reduced anymore");
return false;
}else{
alert("The entered integer must be greater than 1");
formname.username.value =1;
return false;
}
}
function add2(formname){
if (formname.meili.value == "") {
alert("Please enter a positive integer ");
formname.meili.focus();
return false;
}
flag = checkscope(formname);
if(flag==false){
return false ;
}
if( parseInt(formname.meili.value)>0){
formname.meili.value =parseInt(formname.meili.value) 1;
if(parseInt(formname .username.value) parseInt(formname.meili.value)>10){
alert("The sum of the entered integers must not be greater than 10");
formname.meili.value =10-parseInt(formname. username.value);
return false;
}else
return true;
}else{
alert("The entered integer must be greater than 0");
formname.meili. value=1;
return false;
}
}
function subtration2(formname){
if (formname.meili.value == "") {
alert("Please Enter a positive integer");
formname.meili.focus();
return false;
}
flag = checkscope(formname);
if(flag==false){
return false;
}
if( parseInt(formname.meili.value)>1){
formname.meili.value =parseInt(formname.meili.value)-1;
if(parseInt(formname.username.value) parseInt(formname.meili.value)>10){
alert("The sum of the entered integers must not be greater than 10");
formname.meili.value =10 -parseInt(formname.username.value);
return false;
}else
return true;
}else if(parseInt(formname.meili.value) ==1){
alert("It is already the minimum value and cannot be reduced anymore");
return false;
}else{
alert("The entered integer must be greater than 1");
formname.meili.value =1;
return false;
}
}
function checkscope(formname){
if((parseInt(formname.username.value)>9)||(parseInt(formname .username.value)<1)){
alert("username input number exceeds the allowed range 1-9");
formname.username.value=1;
formname.username.focus( );
return false;
}
if((parseInt(formname.meili.value)>9)||(parseInt(formname.meili.value)<1)){
alert("meili input number exceeds the allowed range 1-9");
formname.meili.value=1;
formname.meili.focus();
return false;
}
return true;
}

Put the three files in the same folder, and then try the effect. I am definitely not lying to you.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn