在node.js中制作了一个阅读系统。用户可以将书籍添加到数据库并搜索保存在数据库中的书籍。用户必须插入作者,流派,标题和价格。价格必须是一个数字,系统必须通知用户,以防他插入任何不同的东西。在我的情况下,系统根本不会提醒用户,除非用数字提交价格。应该如何改变?
<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>
发布于 2018-06-12 16:57:09
在你的<form>
元素中,有onsubmit="return ControlPrice"
。需要通过添加一些parens来实际执行该功能()
:
<form onsubmit="return ControlPrice()">
可能不需要return
。
发布于 2018-06-12 18:17:03
在功能上改变你的if条件 (isNaN(+price))
form.price.value; 返回一个字符串值。需要先将其转换为数字格式,然后检查其是否为NaN
有很多方法可以将字符串转换为数字。
也可以使用 (isNaN(parseInt(price)))
https://stackoverflow.com/questions/-100005343
复制相似问题