gpt4 book ai didi

javascript - 表单控件不可聚焦

转载 作者:行者123 更新时间:2023-12-02 20:53:20 26 4
gpt4 key购买 nike

我有一个跨两页的表单。我正在尝试验证电子邮件地址,但每当我输入错误的电子邮件时,我都会在控制台中收到错误消息,指出无效的表单控件不可聚焦

测试代码片段的最简单方法:

  1. 将名字和姓氏留空
  2. 输入电子邮件地址的单个字母
  3. 点击下一步
  4. 点击“提交”
  5. 检查控制台是否有错误消息

这个 stackoverflow 答案 not focusable不回答问题。电子邮件输入没有“REQUIRED”,并且除提交之外的所有按钮都具有 type='BUTTON',但具有与其关联的点击功能。

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com