gpt4 book ai didi

javascript表单验证刷新页面

转载 作者:行者123 更新时间:2023-11-28 02:46:37 25 4
gpt4 key购买 nike

function subVal(){    if (document.forms['loginform'].name.value == '' || document.forms['loginform'].uid.value == '')    {        if (document.forms['loginform'].uid.value == '')    {        document.getElementById('afterpass').innerHTML = 'FORGOT PASSWORD';    }    if (document.forms['loginform'].name.value == '')    {        document.getElementById('aftername').innerHTML = 'FORGOT USERNAME';    }    }    if (document.forms['loginform'].name.value != '' && document.forms['loginform'].uid.value != '')    {        document.forms['loginform'].submit();    }}

当我尝试验证时,它会执行它应该执行的操作(插入 HTML)然后刷新。我怎样才能阻止这个?

最佳答案

<script>
function subVal()
{

if (document.getElementById('name').value == '' && document.getElementById('uid').value == '')
{
document.getElementById('aftername').innerHTML = 'FORGOT USERNAME';

document.getElementById('afterpass').innerHTML = 'FORGOT PASSWORD';
return false;
}
else if ( document.getElementById('uid').value == '' )
{
document.getElementById('afterpass').innerHTML = 'FORGOT PASSWORD';
return false;
}
else if (document.getElementById('name').value == '')
{
document.getElementById('aftername').innerHTML = 'FORGOT USERNAME';
return false;
}
else
{
document.forms['loginform'].submit();
}
}


</script>

<form method='post' name='loginform'>
<input type='text' name='name' id="name" />
<div id='aftername'></div>
<input type='password' name='uid' id="uid" />
<div id='afterpass'></div>
<input type="button" value="Submit" onclick='subVal()'>
</form>

关于javascript表单验证刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825395/

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