如何在树枝中的javascript变量? [英] how to javascript variable in twig?
本文介绍了如何在树枝中的javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的树枝文件中有以下代码:
i have this code in my twig file:
<script>
function trabajoEquipos(id)
{
var cliente = id
$.ajax({
dataType: "json",
url: {{ path("work_trabajoEquipos",{id: cliente}) }},
type: "POST",
data: {"cliente":cliente},
success: function(data)
{
$("#divajax").html(data);
}
})
}
如您所见,我想将var"cliente"放到ajax的URL部分中,但是我不能以这种方式做到这一点
As you can see, i want to put the var "cliente" into the URL section of the ajax, but i cant do it in this way
修改
这是我要求的完整代码:
Here is my full code as requested :
{% extends 'base.html.twig' %}
{% block body %}
{{parent()}}
<div class="container">
<div class="page-header">Escribe los datos del trabajo encargado por el cliente: </div>
<div class="col-md-12">
<div class="row well">
<div style="padding-left:250px; padding-right:250px;">
{{ form_start(form, {'attr' : { 'role' : 'form', 'novalidate' : 'novalidate'} }) }}
<div class="form-group">
{{ form_label(form.titulo) }}
{{ form_widget(form.titulo, {'attr': { 'class' : 'form-control', 'placeholder' : 'Nombre del trabajo' } }) }}
<span class="text-danger">{{ form_errors(form.titulo) }}</span>
</div>
<div class="form-group">
{{ form_label(form.descripcion) }}
{{ form_widget(form.descripcion, {'attr': { 'class' : 'form-control', 'placeholder' : 'Descripcion del trabajo' } }) }}
<span class="text-danger">{{ form_errors(form.descripcion) }}</span>
</div>
<div class="form-group">
{{ form_label(form.fechainicio) }}
{{ form_widget(form.fechainicio, {'attr': { 'class' : 'form-control', 'placeholder' : '' } }) }}
<span class="text-danger">{{ form_errors(form.fechainicio) }}</span>
</div>
<div class="form-group">
{{ form_label(form.fechaentrega) }}
{{ form_widget(form.fechaentrega, {'attr': { 'class' : 'form-control', 'placeholder' : '' } }) }}
<span class="text-danger">{{ form_errors(form.fechaentrega) }}</span>
</div>
<div class="form-group">
{{ form_label(form.client) }}
{{ form_widget(form.client, {'attr': { 'class' : 'form-control', 'onchange' : 'trabajoEquipos(this.value)' } }) }}
<span class="text-danger">{{ form_errors(form.client) }}</span>
</div>
<div class="form-group">
{{ form_label(form.user) }}
{{ form_widget(form.user, {'attr': { 'class' : 'form-control', 'placeholder' : '' } }) }}
<span class="text-danger">{{ form_errors(form.user) }}</span>
</div>
<div class="form-group">
{{ form_label(form.prioridad) }}
{{ form_widget(form.prioridad, {'attr': { 'class' : 'form-control', 'placeholder' : '' } }) }}
<span class="text-danger">{{ form_errors(form.prioridad) }}</span>
</div>
<div class="checkbox">
{{ form_widget(form.terminado) }}<b>Terminado</b>
<span class="text-danger">{{ form_errors(form.terminado) }}</span>
</div>
<p>
{{ form_widget( form.save, { 'label' : ' CREAR TRABAJO', 'attr' : { 'class' : 'btn btn-success' } } ) }}
<a class="btn btn-danger" href="{{ path('user_index')}}">VOLVER<span class="glyphicon glyphicon-menu-left"></span></a>
</p>
{{ form_end(form) }}
</div>
</div>
<div id="divajax" class="row well">
</div>
</div>
</div>
</div>
<script>
function trabajoEquipos(id)
{
var cliente = id
$.ajax({
dataType: "json",
url: {{ path("work_trabajoEquipos",{id: cliente}) }},
type: "POST",
data: {"cliente":cliente},
success: function(data)
{
$("#divajax").html(data);
}
})
}
</script>
{% endblock %}
推荐答案
这将是我解决此问题的方法.由于您无法将javascript var传递给细枝,因此需要提前计算路径.由于我不熟悉symfony表单构建器,因此我创建了一个伪代码
This would be my aproach to solve this issue. As you can't pass a javascript var to twig you need to calculate the paths in advance. As I'm not familiar with the symfony form builder I've created a pseudo code
template.twig
template.twig
<select id="cbo_client" name="cbo_client">
{% for client in clients %}
<option value="{{ client.getId() }}" data-path="{{ path('work_trabajoEquipos', {id: client.getId() }) }}">{{ client.getName() }}</option>
{% endfor %}
</select>
JavaScript:
Javascript :
<script>
$(function() {
$('#cbo_client').on('change', function(e) {
var url = $('option:selected', this).data('path');
$.ajax({
dataType: "json",
url: url,
type: "POST",
data: {cliente: $(this).val(), },
success: function(data)
{
$("#divajax").html(data);
}
})
}
});
</script>
这篇关于如何在树枝中的javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文