显示输入字段
我使用的Symfony,以使一个网球俱乐部的网站,我打我的头向下的东西:显示输入字段
我想显示基于输入字段在下拉列表中选择该选项。
这是场景: 我是网站的管理员,我想预约。如果预定是比赛(从ChoiceType列表中选择),我想显示输入字段以输入比赛名称。
我想要做的东西,看起来像这样在我的树枝观点:
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control' }}) }}
</div>
</div>
{% if reservationType == "tournament" %}
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName) }}
</div>
</div>
{% endif %}
是否有可能这样做只是树枝?
在此先感谢!
您必须使用jQuery来解决这个问题:
$(document).ready(function(){
$('.reservation').change(
var reservation = $(this).val();
if (reservation == 'xxxx'){
$('.tourName').show();
}else{
$('.tourName').hide();
}
);
});
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation' }}) }}
</div>
</div>
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName, {'attr': {'class': 'hidden tourName' }}) }}
</div>
</div>
你好,感谢您的回答! 下面是我结束了:https://jsfiddle.net/rrcpbxeq/ 虽然它不工作,但我很确定我在这里做错了什么... –
是的,在jsfiddle中,你是添加jQuery在JavaScript窗口中。再次检查它现在工作正常。 https://jsfiddle.net/rrcpbxeq/2/ –
谢谢你的作品! –
不,它不可能只与树枝。
你可以做的是将脚本添加到您的模板:
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation-type' }}) }}
</div>
</div>
<script type="text/javascript" src="{{ asset('bundles/yourBundle/theNameOfTheScriptYouPutInRessourcesPublic.js') }}"></script>
然后在脚本(使用jQuery),你只听改变select事件插入输入。
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('<input type="text" />').appendTo($(this).parent('form-group'));
}
));
如果你输入需要枝杈变量或东西隐藏在树枝模板,然后在脚本中,您刚刚从隐藏类型更改为文本或任何你需要,你可以添加输入:
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('input[name="tournament-name"]').prop('type', 'text');
}
));
如果你不想使用JavaScript,你可以考虑使用表单事件侦听器:http://symfony.com/doc/current/form/dynamic_form_modification.html
你需要JavaScript来实现这一点 – DarkBee