简体   繁体   English

隐藏和显示取决于价值

[英]Hide and Show depending on value

Does anyone know how to hide and show options under select -tag depending on a certain value from database? 有谁知道如何根据数据库中的某个值在select -tag下隐藏和显示选项?

Here's what I did but it doesn't work: 这是我所做的,但是不起作用:

<script>
    var a = <?php echo $row["status"]; ?>
    if(a == 'To be check' || a == 'Endorsed By IT') {
        $("#new").show();
    } else
        $("#new").hide();   
    </script>

Here's what's under #new : 这是#new下的#new

<div id="new">
    <select name="status">
        <option value="Request">Request</option>
        <option value="Upload">Upload</option>
    </select>
</div>

As of your problem b0s3 has the answer . 对于您的问题, b0s3有答案 Besides that, I would recommend a different way to handle this problem: 除此之外,我建议使用其他方法来解决此问题:

Try to modify the HTML markup while rendering instead of your altering your JavaScript: 尝试在渲染时修改HTML标记,而不要更改JavaScript:

<div id="new" <?print ( in_array($row["status"], array('To be check', 'Endorsed By IT')) ? 'class="active"' : ''); ?>
    […]
</div>

You can use a CSS class like: 您可以使用CSS类,例如:

#new { display: none; }
#new.active { display: block; }

You can still alter the visibility later by using: 您仍然可以稍后使用以下方法更改可见性:

document.getElementById('new').classList.toggle('active'); // vanilla JS
$('#new').toggleClass('active'); // jQuery

The advantages are: 优点是:

  • The element is hidden right away and won't be visible to the user if your page loads slowly (for whatever reason). 该元素立即隐藏,如果您的页面加载缓慢(无论出于何种原因),则对用户不可见。
  • The JavaScript code could be kept and maintained separately from your template, as it doesn't have to be modified by PHP. JavaScript代码可以与模板分开保存和维护,因为不必通过PHP对其进行修改。

Missing the quotes. 缺少引号。 $row["status"] contains string. $row["status"]包含字符串。 You should add the quotes properly. 您应该正确添加引号。

<script>
    var a = '<?php echo $row["status"]; ?>'; // missing the quotes here
    if(a == 'To be check' || a == 'Endorsed By IT')
    {
        $("#new").show();
    }
    else
        $("#new").hide();

</script>

To show/hide div using javascript, you can write like : 要使用javascript显示/隐藏div,您可以这样编写:

<script>
        var a = '<?php echo $row["status"]; ?>';
        if(a == 'To be check' || a == 'Endorsed By IT')
        {
            document.getElementById(new).style.display = 'block';
        }
        else
            document.getElementById(new).style.display = 'none';

    </script>

Try this:- 尝试这个:-

<script>
 var a = '<?php echo $row["status"]; ?>';
 if(a == 'To be check' || a == 'Endorsed By IT')
 {
 document.getElementById("new").style.display = 'block';
 }
 else
 {
 document.getElementById("new").style.display = 'none';
 }
 </script>

i guess you need to do it write condition in document ready method 我想你需要在文档准备好方法中写条件

$( document ).ready(function() {
var a = '<?php echo $row["status"]; ?>'; // missing the quotes here
    if(a == 'To be check')
     {
         $("#new").show();
     }
     else
         $("#new").hide();
})

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

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