我是想要radio选中N的时候才提示的,现在情况是我没选中或选中Y都提示了,,错在哪里呀?<script>
function beforesubmit()
{
if (document.getElementById("FixYN").value="N")
//if (document.form1.FixYN.value ="N" )
{ if (document.form1.BBPre.value!="")
{
var len1 = document.form1.BBPre.value.length //获取判定碼值的長度
var BBPre1 = document.form1.BBPre.value
var beginno1 = document.form1.beginno.value //獲取beginno的值
var beginno2 = (beginno1.substr(0,len1))
if (BBPre1 != beginno2)
{
alert("首碼與判定碼不對應!")
form1.beginno.focus();
return false;
}}
}
}
</script><form id="form1" name="form1" method="post" action="" onsubmit="return beforesubmit()">
<p>
<label for="BBPre"></label>
<input name="BBPre" type="text" id="BBPre" value="ABCD" />
Y
:
<input type="radio" name="FixYN" id="FixYN" value="Y" />
N:
<label for="FixYN"></label>
<input type="radio" name="FixYN" id="FixYN" value="N" />
<label for="FixYN"></label>
</p>
<p>
<label for="beginno"></label>
<input type="text" name="beginno" id="beginno" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>
function beforesubmit()
{
if (document.getElementById("FixYN").value="N")
//if (document.form1.FixYN.value ="N" )
{ if (document.form1.BBPre.value!="")
{
var len1 = document.form1.BBPre.value.length //获取判定碼值的長度
var BBPre1 = document.form1.BBPre.value
var beginno1 = document.form1.beginno.value //獲取beginno的值
var beginno2 = (beginno1.substr(0,len1))
if (BBPre1 != beginno2)
{
alert("首碼與判定碼不對應!")
form1.beginno.focus();
return false;
}}
}
}
</script><form id="form1" name="form1" method="post" action="" onsubmit="return beforesubmit()">
<p>
<label for="BBPre"></label>
<input name="BBPre" type="text" id="BBPre" value="ABCD" />
Y
:
<input type="radio" name="FixYN" id="FixYN" value="Y" />
N:
<label for="FixYN"></label>
<input type="radio" name="FixYN" id="FixYN" value="N" />
<label for="FixYN"></label>
</p>
<p>
<label for="beginno"></label>
<input type="text" name="beginno" id="beginno" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>
通常这种情况下,是通过getElementsByName()来取DOM数组,再用checked来判断当前radio的value值。
你这个要想方便的话,两个ID设成不同的,直接判断N的checked属性
这个=号是增值语句,判断是不是相等用的是==
<script>
function beforesubmit()
{
if (document.getElementById("FixY").checked)
//if (document.form1.FixYN.value ="N" )
{ if (document.form1.BBPre.value!="")
{
var len1 = document.form1.BBPre.value.length //获取判定碼值的長度
var BBPre1 = document.form1.BBPre.value
var beginno1 = document.form1.beginno.value //獲取beginno的值
var beginno2 = (beginno1.substr(0,len1))
if (BBPre1 != beginno2)
{
alert("首碼與判定碼不對應!")
form1.beginno.focus();
return false;
}}
}
}
</script><form id="form1" name="form1" method="post" action="" onsubmit="return beforesubmit()">
<p>
<label for="BBPre"></label>
<input name="BBPre" type="text" id="BBPre" value="ABCD" />
Y
:
<input type="radio" name="FixYN" id="FixY" value="Y" />
N:
<label for="FixYN"></label>
<input type="radio" name="FixYN" id="FixN" value="N" />
<label for="FixYN"></label>
</p>
<p>
<label for="beginno"></label>
<input type="text" name="beginno" id="beginno" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>