Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中相邻复选框时输入字段验证_Javascript_Jquery_Validation_Checkbox - Fatal编程技术网

Javascript 选中相邻复选框时输入字段验证

Javascript 选中相邻复选框时输入字段验证,javascript,jquery,validation,checkbox,Javascript,Jquery,Validation,Checkbox,对于公共字段“服务”,我有一个具有多个值的表单。我已经为选择创建了一个选项“其他”。我想用“其他”选项旁边的javascript输入字段进行验证。 代码如下 <label for="service" style="font-size:15px; vertical-align:top;">Service You Want*</label> <input type="checkbox" name="service[]" id="Complete new set up

对于公共字段“服务”,我有一个具有多个值的表单。我已经为选择创建了一个选项“其他”。我想用“其他”选项旁边的javascript输入字段进行验证。 代码如下

<label for="service" style="font-size:15px; vertical-align:top;">Service You Want*</label>

<input type="checkbox" name="service[]" id="Complete new set up " value="Complete new set up ">Complete new set up<br>
<input type="checkbox" name="service[]" id="Standardization" value="Standardization ">Standardization<br>
<input type="checkbox" name="service[]" id="Training" value="Training">Training <br>
<input type="checkbox" name="service[]" id="Trouble shooting " value="Trouble shooting ">Trouble shooting <br>
<input type="checkbox" name="service[]" id="Addition of new techniques" value="Addition of new techniques">Addition of new techniques<br>
<input type="checkbox" name="service[]" id="Hands on training" value="Hands on training">Hands on training<br>
<input type="checkbox" name="service[]" id="Seminars & Workshops" value="Seminars & Workshops">Seminars & Workshops<br>
<input type="checkbox" name="service[]" id="Preparations of documents" value="Preparations of documents ">Preparations of documents <br>
<input type="checkbox" name="service[]" id="Other" onclick="enable_text(this.checked)"  value="other" >Other

<input type="text" name="other_text"><br>
您想要的服务*
完成新设置
标准化
培训
故障排除
添加新技术
实践培训
研讨会和研讨会
文件的准备
另外
对于“其他”选项,如果选中“其他”选项,则将启用输入文本字段

用于启用输入字段的工作javascript为:

<script>     
    function enable_text(status)
    {
        status=!status; 
        document.formname.other_text.disabled = status;
    }
</script>

功能启用\u文本(状态)
{
状态=!状态;
document.formname.other_text.disabled=状态;
}
用于选择至少一个服务的javascript正在工作,如下所示:

<script>
    var chks = document.getElementsByName('service[]');
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++)
    {
        if (chks[i].checked)
        {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false)
    {
        alert("Please Select At Least One Service.");
        return false;
    }  
</script>

var chks=document.getElementsByName('service[]);
var hasChecked=false;
对于(变量i=0;i

但如果选中复选框中的“其他”选项,我不知道如何验证输入字段。

尝试以下javascript:

<script>
var chks = document.getElementsByName('service[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please Select AtLeast One Service.");
return false;
}  
function isBlank(str) {
return (!str || /^\s*$/.test(str));
} 

if (document.getElementById("Other").checked &&  isBlank(document.getElementsByName('other_text').value)) {
alert("Please Enter Details ABout Other Services You Want......... ");
document.contactus.other_text.focus();
return false;
}
</script>

var chks=document.getElementsByName('service[]);
var hasChecked=false;
对于(变量i=0;i