gpt4 book ai didi

javascript - 两个文本字段,只需填写一个(任一)

转载 作者:行者123 更新时间:2023-11-28 18:11:20 24 4
gpt4 key购买 nike

所以我的网页中有两个字段,一个用于电话号码,另一个用于电子邮件地址,我需要使用 JavaScript 而不是 jQuery 来填写其中之一。我在这里找到的大多数答案都是针对 jQuery 的,任何使用 JavaScript 的解决方案将不胜感激。谢谢!

  function User_one(){
var phone = document.getElementById('PhoneText2').value;
var mail = document.getElementById('EmailText1').value;
if (phone && mail == ""){
alert("An error occurred.");
}else{
return false;
}
}

最佳答案

使用实际代码更新

我的做法是这样的

(function () {
document.getElementById('myForm').addEventListener('submit', function(event){
// Get the length of the values of each input
var phone = document.getElementById('PhoneText2').value.length,
email = document.getElementById('EmailText1').value.length;

// If both fields are empty stop the form from submitting
if( phone === 0 && email === 0 ) {
event.preventDefault();
}
}, false);
})();
<小时/>

由于您没有提供任何代码供我们使用,我将用伪代码回答:

On form submission {
If (both telephone and email is empty) {
throw validation error
}
otherwise {
submit the form
}
}

如果你向我展示你的代码,我也会向你展示我的:-)

关于javascript - 两个文本字段,只需填写一个(任一),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41476144/

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