我有一个分布在两页上的表格。我正在尝试验证电子邮件地址,但是每当我输入错误的电子邮件时,控制台中都会出现错误,指出无效的表单控件无法聚焦
测试代码段的最简单方法:
留下名字和空白
输入一封电子邮件地址
点击下一步
点击提交
检查控制台是否有错误消息
此stackoverflow答案not focusable不回答问题。电子邮件输入中没有“ REQUIRED”,并且所有按钮的类型都为“ =”,但“提交”却具有单击功能。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
<div id='container'>
<div id='section-container'>
<div id='section1' class='sections'>
<div id='section1b'>
<span class='majCap'>Get Involved</span>
<span class='minCap'>Enter your details below</span>
<form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
<div class='columns'>
<input class='detailsC' type="text" name="fname" placeholder="First Name" id='fname'>
<input class='detailsC' type="text" name="lname" placeholder="Last Name" id='lname'>
<input class='detailsC' type="email" name="email" placeholder="Email..." id='email'>
</div>
<div class='rowsDet'>
<span class='detailsR'><input type="checkbox" name="wres">Weston Resident</span>
<span class='detailsR'><input type="checkbox" name="news">Quarterly Newsletter</span>
</div>
<div class='spacer'></div>
<span class="minCap" >Areas of Interests</span>
<div class='rowsInt'>
<div class="columns">
<span><input type="checkbox" name="energy-int">Energy</span>
<span><input type="checkbox" name="recycling-int">Recycling</span>
<span><input type="checkbox" name="composting-int">Composting</span>
<span><input type="checkbox" name="transport-int">Transportation</span>
</div>
<div class="columns">
<span><input type="checkbox" name="climate-int">Climate</span>
<span><input type="checkbox" name="trees-int">Trees</span>
<span><input type="checkbox" name="pollinators-int">Pollinators</span>
<span><input type="checkbox" name="water-int">Water</span>
<span><input type="checkbox" name="other-int">Other</span>
</div>
</div>
<button id='next' type='button' onclick="nextPage()">next</button>
</div>
<div id='section2' class='sections'>
<span class='minCap' >Current Campaign Information:</span>
<div class='rowsCam'>
<div class="columns">
<span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
<span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
</div>
<div class="columns">
<span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
<span><input type="checkbox" name="cca-cpg">CCA</span>
</div>
</div>
<div class='spacer2'></div>
<input type="submit" value="SUBMIT">
<button id='back' type='button' onclick="backPage()">back</button>
</form>
</div>
</div>
</div>
</div>
<script>
function nextPage(){
var section1b = document.getElementById('section1b');
section1b.style.display='none';
var section2 = document.getElementById('section2');
section2.style.display='block';
}
function backPage(){
var section1b = document.getElementById('section1b');
section1b.style.display='block';
var section2 = document.getElementById('section2');
section2.style.display='none';
}
function mySubmit(x){
event.preventDefault();
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!email.match(mailformat))email="";
if(fname == "" || lname == "" || email == ""){
alert("Please enter name and email");
backPage();
return false;
}else{
//var myForm = document.getElementById('myForm');
//myForm.submit();
return true;
}
}
</script>
</body>
</html>
最佳答案
您只需要更改一行代码即可:-
您必须设置**<input type="text" />**
而不是<input type="email" />
因此,更新的行将是:-
<input class='detailsC' type="text" name="email" placeholder="Email..." id='email'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
<div id='container'>
<div id='section-container'>
<div id='section1' class='sections'>
<div id='section1b'>
<span class='majCap'>Get Involved</span>
<span class='minCap'>Enter your details below</span>
<form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
<div class='columns'>
<input class='detailsC' type="text" name="fname" placeholder="First Name" id='fname'>
<input class='detailsC' type="text" name="lname" placeholder="Last Name" id='lname'>
<input class='detailsC' type="text" name="email" placeholder="Email..." id='email'>
</div>
<div class='rowsDet'>
<span class='detailsR'><input type="checkbox" name="wres">Weston Resident</span>
<span class='detailsR'><input type="checkbox" name="news">Quarterly Newsletter</span>
</div>
<div class='spacer'></div>
<span class="minCap">Areas of Interests</span>
<div class='rowsInt'>
<div class="columns">
<span><input type="checkbox" name="energy-int">Energy</span>
<span><input type="checkbox" name="recycling-int">Recycling</span>
<span><input type="checkbox" name="composting-int">Composting</span>
<span><input type="checkbox" name="transport-int">Transportation</span>
</div>
<div class="columns">
<span><input type="checkbox" name="climate-int">Climate</span>
<span><input type="checkbox" name="trees-int">Trees</span>
<span><input type="checkbox" name="pollinators-int">Pollinators</span>
<span><input type="checkbox" name="water-int">Water</span>
<span><input type="checkbox" name="other-int">Other</span>
</div>
</div>
<button id='next' type='button' onclick="nextPage()">next</button>
</div>
<div id='section2' class='sections'>
<span class='minCap'>Current Campaign Information:</span>
<div class='rowsCam'>
<div class="columns">
<span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
<span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
</div>
<div class="columns">
<span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
<span><input type="checkbox" name="cca-cpg">CCA</span>
</div>
</div>
<div class='spacer2'></div>
<input type="submit" value="SUBMIT">
<button id='back' type='button' onclick="backPage()">back</button>
</form>
</div>
</div>
</div>
</div>
<script>
function nextPage() {
var section1b = document.getElementById('section1b');
section1b.style.display = 'none';
var section2 = document.getElementById('section2');
section2.style.display = 'block';
}
function backPage() {
var section1b = document.getElementById('section1b');
section1b.style.display = 'block';
var section2 = document.getElementById('section2');
section2.style.display = 'none';
}
function mySubmit(x) {
event.preventDefault();
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var email = document.getElementById('email').value;
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (!email.match(mailformat)) email = "";
if (fname == "" || lname == "" || email == "") {
alert("Please enter name and email");
backPage();
return false;
} else {
//var myForm = document.getElementById('myForm');
//myForm.submit();
return true;
}
}
</script>
</body>
</html>
也可以在这里检查我的解决方案-
https://codepen.io/pen/?editors=1010
关于javascript - 表单控件无法聚焦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61550834/