[英]Passing parameters into Javascript in HTML
I have an html template that I am using in my Django project. 我有一个我在Django项目中使用的html模板。 I have a string that is defined in my Django view that I want to pass to javascript in my html template, but I am having trouble getting it to work.
我有一个在我的Django视图中定义的字符串,我想在我的html模板中传递给javascript,但是我无法让它工作。 There are two different strings, and the one displayed depends on the state of the toggle.
有两个不同的字符串,显示的字符串取决于切换的状态。
The strings created in my view (and when properly displayed) look like this : 在我的视图中创建的字符串(以及正确显示时)如下所示 :
ATS_string_true
: ATS_string_true
:
2015-123-01:34:39 CDS_FSW_FLASH_PLAYBK_STOP
2015-123-01:34:41 CDS_FSW_FLASH_REPLAY_REQ TLM_DATA_TYPE=0 START_BLOCK=7 NUM_BLOCKS=3
2015-123-01:34:43 CDS_FSW_FLASH_REPLAY_REQ TLM_DATA_TYPE=1 START_BLOCK=300 NUM_BLOCKS=12
2015-123-01:34:51 CDS_FSW_FLASH_PLAYBK_STOP
and ATS_string_false
: 和
ATS_string_false
:
[Ref task: SAT_F7_COMM_NOMINAL_31_op ] 2015-123-01:34:39 CDS_FSW_FLASH_PLAYBK_STOP
However, the string in the error message looks like this: 但是,错误消息中的字符串如下所示:
[Ref task: <SAT_F7_COMM_NOMINAL_31_op> ] >> 2015-123-01:34:39 CDS_FSW_FLASH_PLAYBK_STOP
[Ref task: <SAT_F7_COMM_NOMINAL_31_op> ] >> 2015-123-01:34:41 CDS_FSW_FLASH_REPLAY_REQ TLM_DATA_TYPE=0 START_BLOCK=7 NUM_BLOCKS=3
[Ref task: <SAT_F7_COMM_NOMINAL_31_op> ] >> 2015-123-01:34:43 CDS_FSW_FLASH_REPLAY_REQ TLM_DATA_TYPE=1 START_BLOCK=300 NUM_BLOCKS=12
My html/js looks like this : 我的html / js看起来像这样 :
{% if ATS %}
<div class="panel panel-heading">
<h3 class="text-center">ATS</h3>
</div>
<div class="panel-body">
<div class="checkbox disabled">
<label>
<input id="ATS_debug" type="checkbox" data-toggle="toggle">
ATS Debug
</label>
</div>
<div id="ATS" style="background-color:black; color:white; padding:20px;">
<p>{{ ATS_string_false | linebreaks }}</p>
</div>
</div>
<script>
$(function() {
$('#ATS_debug').change(function() {
if ($(this).prop('checked')) {
$('#ATS').html({{ATS_string_true}});
} else {
$('#ATS').html({{ATS_string_false}});
}
})
})
</script>
{% endif %}
I'm getting an Uncaught SyntaxError: Unexpected identifier error at $('#ATS').html({{ATS_string_true}});
我收到一个Uncaught SyntaxError:
$('#ATS').html({{ATS_string_true}});
意外标识符错误$('#ATS').html({{ATS_string_true}});
. 。 Can anyone help me out with this?
任何人都可以帮我解决这个问题吗?
Thanks! 谢谢!
You need to wrap a variable {{ATS_string_true}} into "" like this: 您需要将变量{{ATS_string_true}}包装成“”,如下所示:
$('#ATS').html("{{ATS_string_true}}");
Because Django doesn't do it for you). 因为Django没有为你做这件事)。
You need to wrap it in quotes as @Nursultan just answered. 你需要将它包装在引号中,因为@Nursultan刚回答。 The templating engines only concern about what to replace with what .
模板引擎只关注什么取代 什么 。 The
{{ATS_string_true}}
part will be exactly replaced by the value of the variable ATS_string_true
. {{ATS_string_true}}
部分将被变量ATS_string_true
的值完全替换。 Now, have a look at what your code which throws error: 现在,看看你的代码抛出错误:
$("#ATS").html({{ATS_string_true}});
Also, 也,
$("#ATS").html({{ATS_string_false}});
Here consider: var ATS_string_true = "I am the true value";
这里考虑:
var ATS_string_true = "I am the true value";
The .html()
method of jQuery expects the first parameter to be a string. jQuery的
.html()
方法期望第一个参数是一个字符串。 If you do not wrap it in quotes, it will look as: 如果你不用引号括起来,它将看起来像:
$("#ATS").html(I am the true value); // Invalid JS
But if you will add quotes like as suggested; 但是如果你按照建议添加引号; the final processed template will look like:
最终处理的模板如下所示:
$("#ATS").html("I am the true value"); // Valid JS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.