var id_person_value = 4;
$('#message').load("{{ path('MyBundle_ajax_modal_vote', {'id_person' :"+id_person_value+"}) }}", function() {//..
它应该返回
$('#message').load("{{ path('MyBundle_ajax_modal_vote', {'id_person' : 4 }) }}", function() {//..
但这会让我回复:http://mywebsite.com/app_dev.php/+id_person_value+
我怎样才能正确注入值?
非常感谢你的帮助!
答案 0 :(得分:1)
您可以使用转义字符(如\'在字符串中插入单引号和\“用于双引号
答案 1 :(得分:1)
尝试使用格式化功能,而不是将事物连接在一起,这很乏味且容易出错,与其他语言中的printf
或format
类似:
str = format("{{ path('MyBundle_ajax_modal_vote', {'id_person' : {0} }) }}", 4)
不幸的是,Javascript中没有这样的内置,但写起来很容易:
function format(str) {
var args = [].slice.call(arguments, 1);
return str.replace(/{(\d+)}/g, function($0, $1) {
return args[$1]
})
}
有关更多实施,请参阅JavaScript equivalent of Python's format() function?。
答案 2 :(得分:0)
使用简单的双引号来开始和结束字符串“”。如果你想在你的字符串中包含任何引号或双引号,请在引用之前使用转义字符\ 例如,你想要字符串为s = hello“world 那么s =“hello \”“+”World“或s =”hello“+”\“”+“world”