dsyak22488 2016-03-27 01:14
浏览 40
已采纳

PHP - 如果选择选项值等于

I want to comment off .cities whenever I select the value Singaporean however it should get visible If I choose any other nationality apart from singaporean. Is it possible in PHP ?

<div class="cities">New York</div>

<select id="emp_nationality" name='emp_nationality' class='form-control'>
<option  value="Singaporean">Singaporean</option>
<option  value="Albanian">Albanian</option>
<option  value="Algerian">Algerian</option>
<option  value="American">American</option>
</select>

I tried this but for some reason it is not working

<?php

if ($_POST['emp_nationality'] != 'Singaporean') {
echo '<div class="cities">New York</div>';
}


?>

ERROR: Undefined index: emp_nationality

  • 写回答

2条回答 默认 最新

  • doujianzi8521 2016-03-27 01:30
    关注

    You can use an onchange handler in js and hide / show the ".cities" div depending on the value chosen. Note I added a display:none to the .cities since your first option in the select list is Singaporean and the function requires a change to trigger it. Also I would normally separate the onclick as a separate handler - rather than mixing it into the HTML but for this example I did it this way. Does not require PHP which would need the option to be selected on page load and would not easily change the visibility of the div on a different choice. This functions to hide the div if the correct option is selected and show it on other option selections.

        function hideDiv(option)    
        {
            if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
        }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="cities" style="display:none">New York</div>
    
    <select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
    <option  value="Singaporean">Singaporean</option>
    <option  value="Albanian">Albanian</option>
    <option  value="Algerian">Algerian</option>
    <option  value="American">American</option>
    </select>

    <div class="cities" style="display:none">New York</div>
    
    <select id="emp_nationality" name='emp_nationality' class='form-control' onchange="hideDiv(this.value)">
    <option  value="Singaporean">Singaporean</option>
    <option  value="Albanian">Albanian</option>
    <option  value="Algerian">Algerian</option>
    <option  value="American">American</option>
    </select>
    
    
    //js
    function hideDiv(option)    
    {
        if(option=="Singaporean"){$('.cities').hide()}else{$('.cities').show()}
    }
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题