如何根据两个文本框的值进行javascript调用? [英] How do I make a javascript call based on the value of two textboxes?
本文介绍了如何根据两个文本框的值进行javascript调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要提交,如果在Myparents中的文本框清空它将警告Myparents,如果在您的父母的文本框空它将提醒您的父母。我想用Javascript做。我该怎么办?
I Want To Ask That On Submit, If Textbox In Myparents Empty It Will Alert Myparents,If Textbox In Yourparents Empty It Will Alert Your Parents. I Want To Do It With Javascript. How Can I Do This?
<html>
<head><title></title>
</head>
<body>
<form method="post" action="" onsubmit="check()">
<div class="myparents">
<input type="text" class="validate[required]" />
</div>
<div class="yourparents">
<input type="text" class="validate[required]" />
</div>
<input type="submit" value="submit" />
</form>
</body>
</html>
推荐答案
这是我的解决方案。 ..但这可能更好
JavaScript表格验证 [ ^ ]
This was my solution...but this is probably better
JavaScript Form Validation[^]
function check(){
var valid true;
var in1 = document.getElementById("var1");
var in2 = document.getElementById("var2");
if(undefined == in1.value || undefined == in2.value)
valid = false;
if(valid){
var val1 = in1.value;
var val2 = in2.value;
if(val1.length==0 || val2.length == 0){
valid = false;
}
}
if(!valid){
alert("All fields required!");
}
}
让我们一起做:
1.在表格标签中,修改onsubmit事件,当返回为false时,提交将被取消
Let do it together:
1. in the form tag, modify the onsubmit event, when the return is false, the submit will be cancelled
<form method="post" action="" onsubmit="return check()">
2.在myparents和yourparents的输入文本中添加id,以便我们稍后可以在javascript函数中访问它们:
2. add ids to the input texts for myparents and yourparents, so that we can access them in javascript function later on:
<input type="text" id="myparents" class="validate[required]" />
<input type="text" id="yourparents" class="validate[required]" />
3.现在,编写check()函数:
3. Now, write the check() function:
<script type="text/javascript">
function check()
{
var myparents = document.getElementById("myparents").value;
var yourparents = document.getElementById("yourparents").value;
var status = true;
if (!myparents.trim()) {
alert("My parents cannot be blank!");
status = false;
}
if (!yourparents.trim()) {
alert("Your parents cannot be blank!");
status = false;
}
return status;
}
</script>
结束。
The End.
这篇关于如何根据两个文本框的值进行javascript调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文