未加载javascript函数
我的symfony2项目中有这样的代码未加载javascript函数,javascript,jquery,symfony,Javascript,Jquery,Symfony,我的symfony2项目中有这样的代码 {% extends 'DomestosAdminBundle::layout.html.twig' %} {% block title %}DomestosAdminBundle:Parent:edit{% endblock %} {% block content %} <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.cs
{% extends 'DomestosAdminBundle::layout.html.twig' %}
{% block title %}DomestosAdminBundle:Parent:edit{% endblock %}
{% block content %}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var availableTags = {{json|raw}};
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<script type="text/javascript">
$(function() {
function addSchool(value) {
$.ajax({
url: '{{path("parent/school/add",{"parent_id": parent.id})}}'
data: {
tags: value
},
success: function(){ alert("Hello world"); }
error: function(){ alert("Chyba píčo"); }
});
}
});
</script>
{{form_start(form)}}
<div id="semeno">
{% for school in parent.school %}
{{school.name}} <a href="{{path('parent/school/delete', {'parent_id': parent.id, 'school_id' : school.id})}}"><i class="fa fa-times-circle fa-2x"></i></a>
{% endfor %}
</div>
{{form_rest(form)}}
<input id="tags" name="tags"> <a style="cursor: pointer;" onclick="addSchool($('#tags').val()); return false;"><i class="fa fa-plus"></i></a>
<button type="submit" class="btn btn-success pull-right">Save</button>
{{form_end(form)}}
{% endblock %}
{%extends'DomestosAdminBundle::layout.html.twig%}
{%block title%}DomestosAdminBundle:父级:编辑{%endblock%}
{%block content%}
$(函数(){
var availableTags={{json | raw};
$(“#标记”).autocomplete({
资料来源:availableTags
});
});
$(函数(){
学校功能(价值){
$.ajax({
url:“{path(“parent/school/add”,{“parent_id”:parent.id}}”
数据:{
标签:值
},
成功:函数(){alert(“Hello world”);}
错误:函数(){alert(“Chyba píčo”);}
});
}
});
{{form_start(form)}}
{parent.school%中的学校为%s}
{{school.name}
{%endfor%}
{{form_rest(form)}
拯救
{{form_end(form)}}
{%endblock%}
第一个函数availableTags工作正常,但firebug控制台抛出错误,无法找到addschool操作。我知道它的位置是错误的,使用了2次标签,但我在这个JS中迷失了方向 这是因为您的
addSchool
功能的可见性。它在$(function())
内声明,因此在该块外不可见
制作:
学校功能(价值){
$.ajax({
url:{path(“parent/school/add”,{“parent_id”:parent.id}}},
数据:{
标签:值
},
成功:函数(){alert(“Hello world”);},
错误:函数(){alert(“Chyba píčo”);}
});
}
你的
<script type="text/javascript">
$(function() {
function addSchool(value) {
$.ajax({
url: '{{path("parent/school/add", {"parent_id": parent.id})}}'
data: {
tags: value
},
success: function(){ alert("Hello world"); }
error: function(){ alert("Chyba píčo"); }
});
}
});
$(函数(){
学校功能(价值){
$.ajax({
url:“{path(“parent/school/add”,{“parent_id”:parent.id}}”
数据:{
标签:值
},
成功:函数(){alert(“Hello world”);}
错误:函数(){alert(“Chyba píčo”);}
});
}
});
它基本上是相同的代码,但是没有$(function())
包装器,在本例中您不需要它,因为您只需要声明函数
注意:在全局命名空间中声明函数可能被认为是一种不好的做法Move
addSchool
function out$(function(){..})代码>范围。像function addSchool(value){..}
在属性列表数据之后给我SyntaxError:missing},它看起来像function addSchool(value){$.ajax({url:'{path(“parent/school/add”,{“parent\u id:parent.id}})数据:{tags:value},success:function(){
check now,我在url参数EDIT:之后添加了逗号,也在成功之后添加了逗号
<script type="text/javascript">
$(function() {
function addSchool(value) {
$.ajax({
url: '{{path("parent/school/add", {"parent_id": parent.id})}}'
data: {
tags: value
},
success: function(){ alert("Hello world"); }
error: function(){ alert("Chyba píčo"); }
});
}
});