jQuery对象内的提示函数无法正常工作

时间:2016-09-26 01:32:27

标签: javascript jquery html

对不起,如果我在这里说些蠢话,我在jQuery上有点像菜鸟。我正在研究HTML / jQuery中的一个按钮,该按钮应该在单击时触发提示,然后返回另一个数字。问题是当我点击按钮时提示不会触发。

JavaScript的:

$(document).ready(function() {
    $('#Progress-Button').click(function() {
        var question = prompt("Enter number of hours worked:", "Enter here");
        if (isNaN(Number(question)) === false) {
            var width = question * 10;
            return width;
        } else {
          question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
        }
    )
})

HTML:

<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="index.js"></script>
<link rel="stylesheet" href="index.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
</html>
<head>
<title>Ian TEST</title>
</head>
<body>
<div id="main">
    <div id="Progress-BG"></div>
    <div id="Progress-Bar"></div>
    <button id="Progress-Button">Click Me</button>
</div> 
</body>

4 个答案:

答案 0 :(得分:1)

您的代码运行顺利,唯一的问题是您在else

之后没有正确关闭
  

});

尝试下面的代码

$(document).ready(function() {
  $('#Progress-Button').click(function() {
    var question = prompt("Enter number of hours worked:", "Enter here");
    if (isNaN(Number(question)) === false) {
      var width = question * 10;
      return width;
    } else {
      question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
    }
  });
})

答案 1 :(得分:0)

您的else块缺少大括号,并且还缺少一些分号。试试这个:

&#13;
&#13;
$(document).ready(function() {
  $('#Progress-Button').click(function() {
    var question = prompt("Enter number of hours worked:", "Enter here");
    if (isNaN(Number(question)) === false) {
      var width = question * 10;
      alert(width);
      return width;
    } else {
      question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
    <div id="Progress-BG"></div>
    <div id="Progress-Bar"></div>
    <button id="Progress-Button">Click Me</button>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的HTML中看起来有些错误,这些错误可能会对此产生影响。 <script><meta><link>元素应位于<head>内。 <head><body>都应在<html>之内:

<html>
    <head>
        <title>Ian TEST</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="index.js"></script>
        <link rel="stylesheet" href="index.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
    </head>
    <body>
        <div id="main">
            <div id="Progress-BG"></div>
            <div id="Progress-Bar"></div>
            <button id="Progress-Button">Click Me</button>
        </div> 
    </body>
</html>

答案 3 :(得分:-4)

您应该使用:

jQuery(document).ready(function($) {
   enter code here

不是这个

$(document).ready(function() {
enter code here

...