【问题标题】:PHP Loop to create Javascript [closed]PHP循环创建Javascript [关闭]
【发布时间】:2012-11-18 03:23:43
【问题描述】:

编辑:哎呀。谢谢大家!

我有以下代码:

<?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>";
?>

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

我想我应该使用&lt;header&gt; 选项,但我完全迷路了。

欢迎大家多多指教!

谢谢,

H.

【问题讨论】:

  • 这应该是显而易见的。在回显打开的标签后,您将直接关闭您的脚本标签。 &lt;script type=\"text/javascript\"&gt;&lt;/script&gt;
  • 为什么不在javascript中做所有的循环?
  • 我很尴尬!!!!谢谢大家!!!!

标签: php javascript loops header head


【解决方案1】:

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

<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>

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

【讨论】:

  • 干杯 Johathan,感谢。
【解决方案2】:

忘记 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>

【讨论】:

  • 非常感谢,非常感谢。但是,当我这样做时,验证不起作用 - 因此我试图将其弹出到 PHP 循环中。
  • 您在内部循环中使用与外部循环相同的变量。外部循环向上计数,而内部循环向下计数到 0,这意味着在每次外部循环迭代中,在内部循环之后,i 将始终为 0(或 -1 并在 .checked 时返回)。
  • 嗯,好的,修改为for (var 使用p 而不是i - 仍然无法正常工作 - 但是 PHP 迭代了 10 次,这是一种享受。我错过了什么(显然今晚很多,因为我错过了该死的&lt;script&gt;
  • 我根据上面的 php 代码更新了应该如何工作的答案。我不知道你在用这个做什么。从阅读代码来看,您似乎正在循环遍历单选按钮组,如果选中了任何按钮,则为行背景着色并返回函数。我认为您想要的是如果没有填写任何单选按钮,请设置背景颜色并返回。它的编写方式,提交表单的唯一方法是没有选择任何内容。
【解决方案3】:

替换

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

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

【讨论】:

    【解决方案4】:

    您将在第三行关闭 &lt;script&gt; 标记:

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

    在此处删除&lt;/script&gt;,它应该可以工作。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2015-04-15
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 2011-07-30
        • 2020-05-01
        • 2023-03-11
        • 2020-07-18
        • 2018-05-10
        相关资源
        最近更新 更多