dstbtam8732 2011-07-08 08:21
浏览 108
已采纳

在单选按钮内从PHP回调调用JavaScript函数

I have a series of radio buttons, which onClick will either reveal or hide a Div:

Reveal the Div:

<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" id="con4" />

Hide the Div:

<input type="radio" name="con[4]" value="0" onclick="toggleLayer4(!this.checked);" checked="checked" id="con4" />

JavaScript:

function toggleLayer4(val)
        {
            if(val == '1' || val === true)
            {
                document.getElementById('con4').checked = true;
                document.getElementById('con4PSTN').style.display = 'block';
            }
            else if(val == '0' || val === false)
            {
                document.getElementById('con4').checked = false;
                document.getElementById('con4PSTN').style.display = 'none';
            }
        }

Now the problem, when the pag is recalled, I can get the radio button checked like this:

<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" <? if ($conn_count[3] == 1){echo "checked=\"checked\"";}?> id="con4" />

But I need a away of calling the JavaScript function to reveal the div if the radio button is checked, I have tried to echo toggleLayer4(this.checked); within the PHP if statement inside tags, however this just seems to reurn the text in the html??

There must be a way, not really versed in JS.

Cheers, B.

  • 写回答

3条回答 默认 最新

  • dongnao2582 2011-07-08 08:49
    关注

    Here is a plain unobtrusive javascript for you which will save you some work - please notice there are no event handlers on the radios anymore.

    I gave them unique IDs which is mandatory.

    If you ever need to use jQuery for other stuff, this script can be a little more elegant.

    I assume

    <input type="radio" name="con[1]" value="1" id="con1_1" />
    <input type="radio" name="con[1]" value="0" id="con1_0" />
    <input type="radio" name="con[2]" value="1" id="con2_1" />
    <input type="radio" name="con[2]" value="0" id="con2_0" />
    <input type="radio" name="con[3]" value="1" id="con3_1" />
    <input type="radio" name="con[3]" value="0" id="con3_0" />
    <input type="radio" name="con[4]" value="1" id="con4_1" />
    <input type="radio" name="con[4]" value="0" id="con4_0" />
    

    and matching object to have conxPSTN where x is the number in the con[x]

    window.onload=function() {
      var conLength = <?php echo count($con); ?>;
      for (var i=1;i<=conLength;i++) {
        var cons = document.getElementsByName("con["+i+"]");
        for (var j=0;j<cons.length;j++) {
          cons[j].onclick=function() {
            var id = this.id.split("_")[0];
            document.getElementById(id+"PSTN").style.display = (this.value==1)?"block":"none"
          }
          if (cons[j].checked) cons[j].click();
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条