gpt4 book ai didi

javascript - 隐藏的div正在做验证

转载 作者:行者123 更新时间:2023-11-28 13:49:19 26 4
gpt4 key购买 nike

其中有两个单选按钮。

单击此按钮时,将显示和隐藏 div。

单击提交按钮时会发生验证。

当 div 可见时,验证正在进行。

但是当 div 隐藏时,验证也会发生。

如何防止对隐藏的 div 进行验证。

这是我的代码:

    <script type="text/javascript">

function checkRadio(frmName, rbGroupName)
{
var radios = frmName.elements[rbGroupName];
for (var i=0; i <radios.length; i++)
{
if (radios[i].checked)
{
return true;
}
}
return false;
}
function chk_oauth(objForm)
{
var user = objForm.user.value;
var pass = objForm.password.value;

if(user =="")
{
alert("Type email");
return false;
}

if(pass =="")
{
alert("Type pasword");
return false;
}

if (!checkRadio(objForm,"oauth_option"))
{
alert("Please select option");
return false;
}
}
</script>
<form name = "frmOauth" enctype="multipart/form-data" action = "" method = "post" onsubmit="javascript:return chk_oauth(this);">
<label><input type="radio" name="oauth_option" id="oauth_yes" value="Yes" onclick="toggle(this)" >Yes</label>
<label><input type="radio" name="oauth_option" id="oauth_no" value="No" onclick="toggle(this)">No</label>
<div class="oauth_div" id="oauth_div" >
Email Address* <br />
<input type="text" class="input-profile-other" placeholder="Email" id="user" name="user" />
Password * <br/>
<input type="password" class="input-profile-other" type="password" id="password" name="password" placeholder="password" />
</div>
<input class="btn-home-search1" type="submit" value="Go" name="oauth_submit" id="oauth_submit">
</form>

<script type="text/javascript">
var t = document.getElementById('oauth_div');

function toggle(switchElement) {
if (switchElement.id == 'oauth_yes'){
t.style.display = '';
//email.setAttribute('type','email');
t.style.visibility = 'visible';
}else{
t.style.display = 'none';
//email.setAttribute('type','text');
t.style.visibility = 'hidden';
}
}

[].forEach.call( document.forms.frmOauth.oauth_option, function(radio){
if( radio.checked ) {
toggle( radio );
}
});
</script>

最佳答案

在此处向该 div 中的所有字段添加一个类 oauth_class

<form name = "frmOauth" enctype="multipart/form-data" action = "" method = "post" onsubmit="javascript:return chk_oauth(this);">
<label><input type="radio" name="oauth_option" id="oauth_yes" value="Yes" onclick="toggle(this)" >Yes</label>
<label><input type="radio" name="oauth_option" id="oauth_no" value="No" onclick="toggle(this)">No</label>
<div class="oauth_div" id="oauth_div" >
Email Address* <br />
<input type="text" class="input-profile-other oauth_class" placeholder="Email" id="user" name="user" />
Password * <br/>
<input type="password" class="input-profile-other oauth_class" type="password" id="password" name="password" placeholder="password" />
</div>
<input class="btn-home-search1" type="submit" value="Go" name="oauth_submit" id="oauth_submit">
</form>

Javascript

var t = document.getElementById('oauth_div');
function toggle(switchElement) {
if (switchElement.id == 'oauth_yes'){
t.style.display = '';
var temp = document.getElementByClass('oauth_class');
temp.style.display = ''
//email.setAttribute('type','email');
t.style.visibility = 'visible';
}else{
t.style.display = 'none';
//email.setAttribute('type','text');
t.style.visibility = 'hidden';
t.style.visibility = 'hidden';

}
}

关于javascript - 隐藏的div正在做验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933140/

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