简体   繁体   English

从 PHP 在单选按钮内回显

[英]Calling JavaScript function from PHP echo within a radio button

I have a series of radio buttons, which onClick will either reveal or hide a Div:我有一系列单选按钮,onClick 将显示或隐藏一个 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: 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);但是如果选中单选按钮,我需要调用 JavaScript function 来显示 div,我试图回toggleLayer4(this.checked); within the PHP if statement inside tags, however this just seems to reurn the text in the html??在标签内的 PHP if 语句中,但这似乎只是重新生成 html 中的文本?

There must be a way, not really versed in JS.一定有办法,不是很精通JS。

Cheers, B.干杯,B.

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.这是一个普通的不显眼的 javascript,它将为您节省一些工作 - 请注意收音机上不再有事件处理程序。

I gave them unique IDs which is mandatory.我给了他们唯一的 ID,这是强制性的。

If you ever need to use jQuery for other stuff, this script can be a little more elegant.如果您需要将 jQuery 用于其他内容,此脚本可以更优雅一些。

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 ]并匹配 object 以获得 conxPSTN 其中x是 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();
    }
  }
}

If you are echoing from php then you should do like如果您从 php 回声,那么您应该这样做

echo "<script type='text/javascript'>toggleLayer4(this.checked);</script>";

But you cant use this in there.但是你不能在那里使用this You need to get the value of the radio button manually n pass it您需要手动获取单选按钮的值 n 传递它

EDIT编辑

This is the case when you want to call the function explicitly in some part of the code and when the php code is not with the <script> tags.当您想在代码的某些部分显式调用 function 并且 php 代码带有<script>标记时,就会出现这种情况。

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


   <input type="radio" name="con[4]" value="1" onclick="toggleLayer4(1);" checked="checked" id="con4" />
 function toggleLayer4(val)
    {
    if(val){
     document.getElementById('con4PSTN').style.display = 'block';}
    else{
     document.getElementById('con4PSTN').style.display = 'none';
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM