求助:radio选择后 其中一个文本框需要填写数据才可提交
<script language="javascript">
<!--
function CheckForm()
{
var flag = true;
var allRadio = document.form.elements["a_dzsw"];
for(var i = 0;i < allRadio.length;i++){
if(allRadio[i].checked==true && allRadio[i].value =="已开展"){
flag = false;
}
}
if (!flag){
alert("请选择")
}
return flag
}
-->
</script> 这是代码:目前做到的是一个radio如果选择了“已开展”就会弹出个对话框 可是想做到的是 radio选择“已开展”并且另外一个字段填写了数据后就可以提交 ,现在这样的效果是不管另外那个字段是否有数据都会拒绝提交
[解决办法]
另外那个字段,在哪里?是什么TAG?
[解决办法]
你在方法里再验证那个字段填写了没有
如果字段和单选框都符合要求返回true
否则返回false
试试
[解决办法]
试试把下面的方法加到你的代码里:
// 找出 name 为a_qyxxhjh_nd 的 input, 注意它的id是A1
var input = document.getElementById("A1");
if ( ... ) { // 检查 input.value 是否填写了数据
//todo
}
<table width="588" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80" height="24">
<input type="radio" name="a_dzsw" value="已开展" onclick=hh.style.display='block'>
已开展
</td>
<td width="505">
<div id=hh style=display:none>
年度:<input type="text" name="a_qyxxhjh_nd" size="12" id="A1" value="" class="input_on" onfocus="this.className='input_out';this.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_on'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_on'" />
名称: <input type="text" name="a_qyxxhjh_mc" size="12" value="" id="A1" class="input_on" onfocus="this.className='input_out';this.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_on'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_on'" />
</div>
</td>
</tr>
<tr>
<td height="24" colspan="2">
<input type="radio" name="a_dzsw" value="未开展" checked onclick=hh.style.display='none'>
未开展
</td>
</tr>
</table>
<INPUT TYPE="button" VALUE=" 提交" ONCLICK="CheckForm()">
function CheckForm()
{
var allRadio = document.getElementsByName("a_dzsw");
var i;
for( i = 0; i < allRadio.length; i++) {
if(allRadio[i].checked && allRadio[i].value =="已开展"){
//首先,页面元素的 ID 不能够重复
//这个地方最好用 getElementByID("a_qyxxhjh_nd") 来获取,但是因为上面的原因……
var nd = document.getElementsByName("a_qyxxhjh_nd")[0];
//正则表达式控制 年度为空或空格之类的弹提示并选中内容并中断
var reg = /^\s*$/
if ( reg.test(nd.value) ) {
alert("请填写年度");
nd.select();
return;
}
}
}
alert("成功提交!");
}
var a = document.getElementById("A1").value;
if(a!=""
[解决办法]
a!=null)
{
//提交
}