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条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行