Javascript函数设置';显示';属性在某些情况下有效,而在其他情况下无效

Javascript函数设置';显示';属性在某些情况下有效,而在其他情况下无效,javascript,php,html,css,Javascript,Php,Html,Css,在我的程序中,我根据用户选择的一组单选按钮显示或不显示某些元素。我使用“onclick”atribute调用Javascript函数“hideshow(e,x)”,该函数将要显示/不显示的元素的类名作为第一个参数,并将“none”或“inline”作为第二个参数。这似乎很有效 但有时,我已经知道应该显示哪些元素,所以我不显示单选按钮,只想直接调用hideshow(e,x)。通过在函数中添加一条ALERT语句,我知道函数实际上正在被调用,并且正在接收正确的参数。然而,这些元素的可视性并没有改变 谁

在我的程序中,我根据用户选择的一组单选按钮显示或不显示某些元素。我使用“onclick”atribute调用Javascript函数“hideshow(e,x)”,该函数将要显示/不显示的元素的类名作为第一个参数,并将“none”或“inline”作为第二个参数。这似乎很有效

但有时,我已经知道应该显示哪些元素,所以我不显示单选按钮,只想直接调用hideshow(e,x)。通过在函数中添加一条ALERT语句,我知道函数实际上正在被调用,并且正在接收正确的参数。然而,这些元素的可视性并没有改变

谁能解释一下,告诉我怎么解决

这是代码。我使用变量$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>

能见度测试
函数隐藏(元素,x){
警报(“在隐藏状态下,元素:+element+”,x:+x);
$(元素).css(“显示”,x);
var telement=元素+“-”;
var tx=“”;
如果(x==“none”){tx=“inline”}其他{tx=“none”}
$(远程通讯).css(“显示”,tx);
}

将输出JavaScript的PHP代码放在需要显示/隐藏的元素下面


在呈现元素之前正在执行函数。您确实包含了超时,但它仅设置为1毫秒。向下移动脚本应该可以完全消除超时。

将输出JavaScript的PHP代码放在需要显示/隐藏的元素下面


在呈现元素之前正在执行函数。您确实包含了超时,但它仅设置为1毫秒。向下移动脚本可以让您完全摆脱超时。

作为一种旁白,使用
console.log(var)
通常比
alert()更方便。
为什么要使用php输出缓冲打印简单字符串?这是不必要的。使用jQuery的问题是,它允许您执行类似于
$('.someclassthatnotnottexist')的操作。dosomethingher()
。。。这绝对没有错误,因为jquery就是这样运行的。。。如果您使用palin ol'javascript等效工具尝试这一点,您可能需要在盲目使用元素之前测试是否有任何元素可以使用。另外,使用
console.log(var)
通常比
alert()
更方便。为什么要使用php输出缓冲打印简单字符串?这是不必要的。使用jQuery的问题是,它允许您执行类似于
$('.someclassthatnotnottexist')的操作。dosomethingher()
。。。这绝对没有错误,因为jquery就是这样运行的。。。如果你用佩林·奥尔·javascript的等价物来尝试,你可能需要在盲目地使用它们之前测试一下是否有任何元素可以使用。谢谢。这很有魅力。(我想这应该是显而易见的。)谢谢。这很有魅力。(我想这应该是显而易见的。)