为什么在输入type =“ number”的情况下我无法获取数据

时间:2019-07-26 11:44:41

标签: html node.js express ejs

我正在使用ejs将数据提取到html文件中。输入<script>时无法提取数据,如果将其更改为输入type="number",则表示正在获取数据。请让我知道输入type="text"

出了什么问题
type="number"

输入<%- include('../includes/head.ejs') %> <link rel="stylesheet" href="/css/forms.css"> <link rel="stylesheet" href="/css/product.css"> </head> <body> <%- include('../includes/navigation.ejs') %> <main> <form class="product-form" action="/admin/<% if(editing){ %> edit-product <% } else { %> add-product <% } %>" method="POST"> <div class="form-control"> <div class="form-control"> <label for="price">Price</label> <input type="text" name="price" id="price" step="0.01" value="<% if(editing){ %> <%= product.price %> <% } %>"> </div> <button class="btn" type="submit"><% if(editing) {%> Edit Product <% } else { %> Add Product <% } %></button> </form> </main> <%- include('../includes/end.ejs') %> 时,应该可以将数据提取到输入框中

4 个答案:

答案 0 :(得分:0)

删除EJS标签之间的空间。

有空格:

<input type="number" name="price" id="price" step="0.01" value="<% if (editing) { %> <%= product.price %> <% } %>">

已删除空间:

<input type="number" name="price" id="price" step="0.01" value="<% if (editing) { %><%=product.price %><% } %>">

答案 1 :(得分:0)

删除 value 属性中的 " "

所以,你可以试试这个:

<input type="text" name="price" id="price" step="0.01" value=<% if(editing){ %> <%= product.price %> <% } %>>

答案 2 :(得分:0)

嘿伙计,我发现我解决了同样的问题,这完全是关于间距

//suppose u are just displaying like this e.g below
   <input type="number" name="cus_stateCode" placeholder="Enter StateCode" id="cus_stateCode" value="<%=foundItem.stateCode%> ">

这就是我们通常在上面写什么错误的方式,例如,如果你像下面那样删除这个空格,则在 %> 和 "> 之间的末尾有多余的空格,例如

   <input type="number" name="cus_stateCode" placeholder="Enter StateCode" id="cus_stateCode" value="<%=foundItem.stateCode%>">

我们就能看到想要的结果 我希望这个例子对你有帮助。

答案 3 :(得分:-1)

  

这是因为type =“ number”只允许数字。

,可能是product.price包含任何非十进制值,这就是为什么您没有获取数据的原因。因此,您应该将数据解析为数字。

尝试:

    <input type="text" name="price" id="price" step="0.01" value="<% if(editing){ %> <%= parseInt(product.price) %> <% } %>">