【问题标题】:If option value is null the hide select tag如果选项值为空,则隐藏选择标签
【发布时间】:2020-08-10 18:15:20
【问题描述】:

实际上我是动态获得价值的。 when option value is null it will hide select tag> if option value is available the show select tag.

<select id="userList">
        <option selected="selected" value="">Choose one</option>
        <?php
            foreach($_SESSION ["userList"] as $key => $value) { ?>
            <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
        <?php

        } ?>
    </select>
----------
if ($("#userList").val() == NULL) {
  $('#userList').hide();
} else {
  $('#userList').show();
}

when option value is null it will hide select tag> if option value is available the show select tag.

【问题讨论】:

  • 您检查控制台是否有错误?如NULL is not defined。试试null
  • @freedomn-m 是的,它显示...
  • 当您第一次使用 php 添加选项时,您可以检查 $_SESSION ["userList"] 是否为空,然后隐藏该选项,否则显示。

标签: javascript jquery


【解决方案1】:

你可以试试这个吗:

var optionsNull = $("#userList").find("option[value='']");

if (optionsNull) {
  optionsNull.hide();
}

【讨论】:

  • @VickyNehare 请检查 DOM 并告诉我渲染输入的 value 属性的值是多少。您可以使用开发人员工具进行检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多