首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js错误如何怎么解决?

Node.js错误如何怎么解决?
EN

Stack Overflow用户
提问于 2018-06-12 08:23:28
回答 2查看 0关注 0票数 0

在node.js中制作了一个阅读系统。用户可以将书籍添加到数据库并搜索保存在数据库中的书籍。用户必须插入作者,流派,标题和价格。价格必须是一个数字,系统必须通知用户,以防他插入任何不同的东西。在我的情况下,系统根本不会提醒用户,除非用数字提交价格。应该如何改变?

代码语言:javascript
复制
<fieldset>
  <legend>Register a book</legend>
  <form action="(My Url)" onsubmit="return ControlPrice" id="post" method="Post">
    Author:<br>
    <input type="text" name="author"><br>
    Title:<br>
    <input type="text" name="title"><br>
    Genre:<br>

    <select name="genre">
      <option value="Science fiction">Science fiction</option>
      <option value="Satire">Satire</option>
      <option value="Drama">Drama</option>
      <option value="Action and Adventure">Action and Adventure</option>
      <option value="Romance">Romance</option>
      <option value="Mystery">Mystery</option>
      <option value="Horror">Horror</option>
    </select>
    <br>
    Price:<br>
    <input type="text" name="price"><br><br>
    <input type="submit" id="submit" value="Submit"><br>
  </form>
</fieldset>
<br>
<fieldset>
  <legend> Search for a book by keyword</legend>
  <br>
  <form action="(MY URL)" method="GET">
    Search for a book:<br>
    <input type="text" name="keyword" <br="">
    <input type="submit" id="search" value="Search" <br="">
  </form>
  <script>
    function ControlPrice() {
      var form = document.getElementById("post");
      var price = form.price.value;
      if (isNaN(price)) {
        alert("Something went wrong! Price Input must be a number");
        return false;
      }
    }
  </script>
</fieldset>
EN

回答 2

Stack Overflow用户

发布于 2018-06-12 16:57:09

在你的<form>元素中,有onsubmit="return ControlPrice"。需要通过添加一些parens来实际执行该功能()

代码语言:javascript
复制
<form onsubmit="return ControlPrice()">

可能不需要return

票数 0
EN

Stack Overflow用户

发布于 2018-06-12 18:17:03

在功能上改变你的if条件 (isNaN(+price))

form.price.value; 返回一个字符串值。需要先将其转换为数字格式,然后检查其是否为NaN

有很多方法可以将字符串转换为数字。

也可以使用 (isNaN(parseInt(price)))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档