1

编辑:哎呀。谢谢大家!

我有以下代码:

<?php
    echo"
    <script type=\"text/javascript\">

    function validation() {

    var ok = 0;
    ";

    for ($i=1; $i<=10; $i++)
      {
        echo"   for (i=document.frmSurvey.q".$i.".length-1; i > -1; i--) {
                if (document.frmSurvey.q".$i."[i].checked) {
                    v = i; i = -1;
                }
            }
            if (i == -1) {
                ok = 1;
                document.getElementById(\"rq".$i."\").style.backgroundColor = '#901F39';
                document.getElementById(\"rq".$i."\").style.color = '#FFF';
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';
                return false;
            }";
        }


    echo "if (ok == 0) document.frmSurvey.submit();
    }
    </script>";
?>

此代码位于<head>我页面的部分中。但是,它只是将文本回显到页面上,而不是实际创建Javascript.

我想我应该使用一个<header>选项,但我完全迷路了。

欢迎任何和所有建议!

谢谢,

H。

4

5 回答 5

4

忘记 PHP:

<script type="javascript">

    function validation() {
        var ok = 0;

        for (var i=1 i<=10; i++){
            for (var j=document.frmSurvey['q'+i].length-1; j > -1; j--) {
                if (document.frmSurvey['q'+j][i].checked) {
                    v = j; j = -1;
                }
            }
            if (j == -1) {
                ok = 1;
                document.getElementById('rq'+i).style.backgroundColor = '#901F39';
                document.getElementById('rq'+i).style.color = '#FFF';
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';
                return false;
            }
        }

        if (ok === 0) {
            document.frmSurvey.submit();
        }
    }
</script>
于 2012-11-29T22:54:37.767 回答
0

为什么不将 JS 代码放在<script>标签内,而不在 PHP 标签内使用它。您可以使用 PHP 标记,仅用于来自服务器端或 PHP 代码的变量。

于 2012-11-29T22:50:38.243 回答
0

您将<script>在第三行关闭您的标签:

<?php
    echo"
    <script type=\"text/javascript\"></script>

删除</script>此处,它应该可以工作。

于 2012-11-29T22:53:25.550 回答
0

代替

<script type=\"text/javascript\"></script>

<script type=\"text/javascript\">
于 2012-11-29T22:54:06.033 回答
0

只是一个远射,但这是我认为你想要/需要的。

<script type="javascript">
    function validation() {
        var err=0,
            ok,i,j;

        //loop through groups of radio buttons
        for (i=1 i<=10; i++){
            //reset ok for this loop
            ok=0;

            //loop through radio buttons in this group
            for (j=document.frmSurvey['q'+i].length-1; j >= 0; j--) {
                //if the button is checked...
                if (document.frmSurvey['q'+j][i].checked) {
                    //set ok to 1
                    ok=1;

                    //break out of loop because we know one is checked, no need to continue on to rest
                    break;
                }
            }

            //if there were no buttons checked
            if (o==0) {
                //color row background and text
                document.getElementById('rq'+i).style.backgroundColor = '#901F39';
                document.getElementById('rq'+i).style.color = '#FFF';
                //color something else background and text
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';

                //increment our error counter
                err++;
            }
        }

        //if there were no errors
        if(err==0){
            //submit the form
            document.frmSurvey.submit();
        } else {
            //tell them how many errors there were
            alert("There were "+err+" errors. Please fix them and try again.");
        }
    }
</script>

循环遍历单选组,然后每个单选按钮查找没有至少一个选中值的任何单选组。如果该组没有检查值,则更改背景和文本颜色并增加错误计数器。如果没有错误,则提交表单,否则会发出警报,说明有多少错误。

于 2012-11-29T23:31:05.207 回答