javascript - jQuery - 如果没有选择任何选项不起作用则发出警报

标签 javascript jquery html css

我有一个功能,当您单击“播放”按钮时,选项和按钮就会消失。

我尝试添加一个 if 语句,说明如果未选择任何内容,则屏幕上会出现警告并且游戏不会启动。但是,这不起作用。

这是我的代码:

$(document).ready(function() {

  $('#options').change(function() { // NOTE: all themes have capital letters on colors
    if ($(this).val() === 'Dark') {
      $("#game").css("background-color", "black");
      $("#player").css({
        "background-color": "white" // using CSS function in case you want to add other stuff
      });
    }
    if ($(this).val() === 'Light') {
      $("#game").css("background-color", "white");
      $("#player").css({
        "background-color": "black" // using CSS function in case you want to add other stuff
      });
    }
  });

  $("#play").click(function() {
    if ($("#options").val != "none") { // NOTE: not working
      $(this).hide();
      $("#options").hide();
      $("#player").show();
    } else if ($("#options").val === "none") {
      alert("You haven't selected a theme")
    }
  });


});
body {
  background-color: #FFFFFF;
  font-family: helvetica, sans-serif;
}
#main_title {
  font-size: 4em;
  color: #000;
  text-shadow: 2px 2px #999999;
}
.block {
  width: 60px;
  height: 60px;
  position: absolute;
}
#game {
  width: 1000px;
  height: 550px;
  border: 3px solid #000000;
  margin: 2% 10%;
  position: relative;
}
#player {
  width: 40px;
  height: 40px;
  left: 50%;
  bottom: 0;
  position: absolute;
}
#play {
  padding: 1%;
  color: white;
  background-color: black;
  border-style: solid;
}
#options {}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Trash Fall - Game</title>

  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <!-- adding jQuery to the script -->
  <script src="script.js"></script>
</head>

<body>

  <h1 id="main_title">Color Drop</h1>



  <button id="play">Play</button>
  <select id="options">
    <option value="none">none (choose one in order to play)</option>
    <option value="Dark">Dark</option>
    <option value="Light">Light</option>
    <option value="Blue_White">Blue/White</option>
    <option value="Red_White">Red/White</option>
  </select>

  <div id="game">

    <div id="player"></div>

  </div>

</body>

</html>



为什么这不起作用?

最佳答案

在您的代码中这一行:

if ($("#options").val != "none") { // NOTE: not working
    ...

您正在使用 .val,因为它是一个属性。尽管它是一种方法 ( .val() ):

if ($("#options").val() != "none") { // `val` --> `val()`
   ...

你似乎清楚地知道这一点;您可能只是忘记或错过了括号。

关于javascript - jQuery - 如果没有选择任何选项不起作用则发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766684/

相关文章:

javascript - 在嵌套对象上实现代理

javascript - $(window).resize() 和 $(document).ready() 计算不同的值

javascript - 使用 Ajax 加载 html 后调用 jquery 插件

javascript - 在 bootstrap 3 中更改药丸颜色并且导航药丸可以折叠

html - 在 CSS 中将 Div 行转换为列

javascript - youtube嵌入式url设置无法自动播放视频

javascript - 是否可以简化使用 javascript 构建的类似矩阵的列选择?

jquery - 为什么我的响应式菜单在宽度调整大小/更改时没有显示?

jquery - 如何通过主干事件处理程序获取触发事件的元素

javascript - 自定义事件和postMessage的区别