[英]How to pass a variable from JavaScript to HTML?
I have a JavaScript defined in my file as follows: 我在文件中定义了一个JavaScript,如下所示:
<script language="javascript">
var transports = "<%=api.transports%>";
var httpsTransport;
var httpTransport;
var splittedTransports= transports.split(',');
for(i = 0; i < splittedTransports.length; i++)
{
if(splittedTransports[i]=="https") {
httpsTransport="https";
} else if (splittedTransports[i]=="http") {
httpTransport="http";
}
}
</script>
And I would like to read it in my HTML page like: 我想在我的HTML页面中阅读它,例如:
<div class="checkbox">
<label class="checkbox inline">
<input type="checkbox" id="transport_http" name="transport_http" value="http" <%if(httpTransport=="http"){%>checked<%}%> />
</label>
<label class="checkbox inline">
<input type="checkbox" id="transport_https" name="transport_https" value="https" <%if(httpsTransport=="https"){%>checked<%}%>/>
</label>
</div>
But now I get an error that states: 但是现在我得到一个错误,指出:
org.mozilla.javascript.EcmaError: ReferenceError: "httpTransport" is not defined.
What am I doing wrong here? 我在这里做错了什么?
I want to allow user to select an checkbox and save the form, and when he tries to edit the form, I want to show what he has saved in his previous operation. 我想允许用户选择一个复选框并保存表单,当他尝试编辑表单时,我想显示他在先前操作中保存的内容。 So, when he tries to edit I try to read the values form backend and would like to show that particular option as checked.
因此,当他尝试编辑时,我尝试读取后端的值,并希望将其显示为选中状态。
<script language="javascript">
var transports = "<%=api.transports%>";
var splittedTransports= transports.split(',');
for(i = 0; i < splittedTransports.length; i++)
{
if(splittedTransports[i]=="https"){
document.getElementById("transport_https").checked = true;
}else if (splittedTransports[i]=="http"){
document.getElementById("transport_http").checked = true;
}
}
</script>
HTML : HTML:
<div class="checkbox">
<label class="checkbox inline " >
<input type="checkbox" id="transport_http" name="transport_http" value="http" />
</label>
<label class="checkbox inline" >
<input type="checkbox" id="transport_https" name="transport_https" value="https"/>
</label>
</div>
The variables in your code are declared , but not defined . 在你的代码的变量声明 ,但没有 定义 。 Give them a random value first, and then update it with the
if
首先给他们一个随机值,然后使用
if
更新
<script language="javascript">
var transports = "<%=api.transports%>";
var httpsTransport = 'no';
var httpTransport = 'no';
var splittedTransports= transports.split(',');
for(i = 0; i < splittedTransports.length; i++)
{
if(splittedTransports[i]=="https"){
httpsTransport="https";
}else if (splittedTransports[i]=="http"){
httpTransport="http";
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.