我试图将从服务器调用的变量存储为表单的默认值。我使用的for循环正确显示所有数据,甚至直接在段落内调用数据也显示数据。但我得到了一个"无法阅读财产'电子邮件'未定义"。所有这些都在.ejs文件中。
<body onload="getEmail()">
// Testing retrieving email - Works
<p><%= garage[0].email %></p>
// Function and Form to plugin email into form - Failing on undefined email
<script>
function getEmail() {
var email = <%= garage[0].email %>;
document.getElementById('myemail').value = email;
}
</script>
<div class="form">
<form action="http://ec2-52-23-218-165.compute-1.amazonaws.com:3000/admingarage" method="post">
<input class="input" data-name="uname" id="myemail" maxlength="256" name="uname" type="email">
<input class="btn-floating btn-large waves-effect waves-light red" data-wait="Please wait..." type="submit" name="action"><i class="material-icons">replay</i></input>
</form>
</div>
// Displaying Garage - Works
<div class="row">
<ul class="garage">
<% for(var i=0; i < garage.length; i++) {%>
<div class="col s2">
<div class="card blue-grey">
<div class="card-content white-text">
<span class="card-title">Car <%= i + 1 %></span>
<p><span><b>Year: </b><%= garage[i].year %></span></p>
<p><span><b>Make: </b><%= garage[i].make %></span></p>
<p><span><b>Model: </b><%= garage[i].model %></span></p>
<p><span><b>Exterior: </b><%= garage[i].exterior %></span></p>
<p><span><b>Interior: </b> <%= garage[i].interior %></span></p>
</div>
</div>
</div>
<% } %>
</ul>
</div>
</body>
&#13;
答案 0 :(得分:0)
这应该可以解决问题
document.getElementById('myemail').value = <%- JSON.stringify(garage[0].email) %>;