Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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
PHP-验证单选按钮警报_Php_Javascript_Radio - Fatal编程技术网

PHP-验证单选按钮警报

PHP-验证单选按钮警报,php,javascript,radio,Php,Javascript,Radio,我在表单中输入了三个单选按钮。当用户单击“提交”而不选择或回答所有问题时,会出现“我想要提醒”框 这是我的表格 <tr> <th> Your attendance<font size="4" > </font></th> <td> <input type="radio" name ="v1" value = "4" onclick="updateTotal();"/></td> <td&

我在表单中输入了三个单选按钮。当用户单击“提交”而不选择或回答所有问题时,会出现“我想要提醒”框

这是我的表格

<tr>
<th> Your attendance<font size="4" > </font></th>
<td>  <input type="radio" name ="v1" value = "4"    onclick="updateTotal();"/></td>
<td>  <input type="radio" name ="v1" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v1" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v1" value = "1"    onclick="updateTotal();" /></td>    
</tr>

<tr>
<th > Your grades  <font size="4" > </font></th>
<td>  <input type="radio" name ="v2" value = "4"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "1"    onclick="updateTotal();" /></td>    
</tr>

<tr>
<th >Your self-control <font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "3"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "2"    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v3" value = "1"    onclick="updateTotal();" /></td>    
</tr> 

您的出席
你的成绩
你的自制力
我已经尝试过这个javascript代码。。但它不起作用,不会出现警报

<script type="text/javascript">
 function validateform()
  {
    if(document.getElementsByName('v1').value="")
    {
      alert('Your grades is not selected');
    return false;
    }
  }
    </script>

函数validateform()
{
if(document.getElementsByName('v1').value=”“)
{
警告(“未选择您的成绩”);
返回false;
}
}

您的比较只有一个
=
,而不是
=
。您正在分配和测试
,这是错误的,因此您的if语句将永远不会执行

比较两个值的运算符是
==

使用以下命令:

if (document.getElementsByName('v1').value== "") {
  // ...
}

使用此函数,您不能在无线组上使用
document.getElementsByName('v1').value

看看这把小提琴:

功能已检查(名称)
{
var buttons=document.getElementsByName(名称);
对于(变量i=0;i
这是因为
getElementsByName()
将返回一个DOM元素数组,因此无法获取值或检测是否直接从此数组中检查元素。您必须遍历所有元素,检查是否至少检查了一个元素。比如说:

function validateForm() {
    var radiosToValidate = ['v1', 'v2', 'v3'];
    var invalidRadios = [];
    for (i = 0; i < radiosToValidate.length; i++) {
        var currentRadioArray = documentGetElementsByName(radiosToValidate[i]);
        var currentRadioIsValid = false;
        for (j = 0; j < currentRadioArray.length; j++) {
            if (currentRadioArray[j].checked) {
                currentRadioIsValid = true;
                break;
            }
        }
        if (currentRadioIsValid === false) {
            invalidRadios[] = radiosToValidate[i];
        }
    }
    if (invalidRadios.length > 0) {
        alert('At least one radio button has not been selected');
        return false;
    }
    return true;
}
函数validateForm(){
var RADIOTOVALIDATE=['v1','v2','v3'];
var invalidRadios=[];
对于(i=0;i0){
警报(“至少有一个单选按钮未被选择”);
返回false;
}
返回true;
}

在哪里调用
validateform()
?这与PHP有什么关系?我注意到一些输入错误。可能会再试一次。或者至少在调试器中查看它。这种方法是正确的,但我没有尝试自己运行,因此可能仍然存在打字错误。因此,请更新我的小提琴以包含您的代码,我们将看到哪里出了问题。
function validateForm() {
    var radiosToValidate = ['v1', 'v2', 'v3'];
    var invalidRadios = [];
    for (i = 0; i < radiosToValidate.length; i++) {
        var currentRadioArray = documentGetElementsByName(radiosToValidate[i]);
        var currentRadioIsValid = false;
        for (j = 0; j < currentRadioArray.length; j++) {
            if (currentRadioArray[j].checked) {
                currentRadioIsValid = true;
                break;
            }
        }
        if (currentRadioIsValid === false) {
            invalidRadios[] = radiosToValidate[i];
        }
    }
    if (invalidRadios.length > 0) {
        alert('At least one radio button has not been selected');
        return false;
    }
    return true;
}