用于设置"display"属性的Javascript函数在某些情况下有效,而在其他情况下则不然



在我的程序中,我要么根据用户选择一组无线电按钮显示某些元素。我使用" on Click" atribute调用JavaScript函数" hideshow(e,x)",该函数作为第一个参数作为要显示/不显示的元素的类名称,以及第二个参数'none'或nonno'或'排队'。似乎正常工作。

但是有时候,我已经知道应该显示哪些元素,因此我不显示无线电按钮,只想直接致电Hideshow(E,X)。通过将警报语句放在函数中,我知道该功能实际上是被调用的,并且正在接收正确的参数。然而,这些元素的可见性不会改变。

任何人都可以解释一下,并告诉我如何修复它?

这是代码。我正在使用变量$ isperson来确定收音机是否:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Visibility Test</title>
    <script>
    function hideshow(element,x){
        alert ("in hideshow, element:"+element+", x:"+x);
        $(element).css("display",x);
        var telement = element+"-";
        var tx="";
        if(x=="none"){tx="inline";}else{tx="none";}
        $(telement).css("display",tx);
    }
</script>
</head>
<body>
<?
$isperson=1;
        if($isperson){
            echo " ready to use hideshow <br>";
            ob_start();
            echo "<script>setTimeout(hideshow('.personInput','inline'),1)</script>";
            ob_end_flush();
         }else{
            echo "<input type='radio' name='bizorper' value='business' onclick="" . "hideshow('.personInput','none')" . "" id='business' checked> Business/organization";
            echo "<input type='radio' name='bizorper' value='person' onclick="" . "hideshow('.personInput','inline')" . "" id='person'> person <p>";
         }
?>
    <p class="personInput" style="display: none"> Normally doesn't display if radio button is 'business', but should if radio button is 'person' or $isperson=1<p></p>  
    <p class="personInput-" style="display: inline">Normally Displays, unless radio button is 'person' or $isperson=1</p> 
    <p class="personInput" style="display: none">Normally doesn't display 2;</p>
</body>
</html>

放置输出JavaScript 下方的PHP代码 需要显示/隐藏的元素。

在渲染元素之前,您的功能要执行。您确实包含了一个超时,但仅设置为1毫秒。向下移动脚本应该使您完全摆脱超时。

相关内容

最新更新