gpt4 book ai didi

javascript - 如何使用 Symfony2 中的警报消息通过 javascript 显示 html twig 表单字段值?

转载 作者:行者123 更新时间:2023-12-02 17:01:51 25 4
gpt4 key购买 nike

我想知道如何使用 Symfony2 中的警报消息通过 javascript 显示 html twig 表单字段值。这是表单代码:

     <html>
<head>
<title> Wkayet </title>
<link rel="shortcut icon" href="{{asset('bundles/ikprojhome/images/icon-WKAYET.png')}}">
<link rel="stylesheet" type="text/css" href="{{asset('bundles/ikprojhome/css2/css.css')}}"/>
<script src='{{asset('bundles/ikprojhome/lib/jquery.min.js')}}'></script>

<script>
function f1(){
alert($('#ikproj_groupebundle_eventsgroupe_start').val());
}
</script>
</head>
<body>
<center>
<div id="container">
<div id="header">

</div>
<div id="content">
<table width="100%" height="100%" align="center">
<tr>
<td>
{% for x in groupe%}
<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:x['id']})}}' method="POST" {{ form_enctype(form) }} onsubmit="f1();">
<!--<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:x['id']})}}' method="POST" {{ form_enctype(form) }} >-->
{% endfor %}
{{ form_errors(form) }}
<table align="center">
<tr>
<td class="separation"><label for="groupname">Titre</label></td>
<td>
<!--<input id="titre" name="titre" required="required" type="text" size="50"/> -->
<div>
{{ form_errors(form.title) }}

{{ form_widget(form.title) }}
</div>
</td>
</tr>
<tr>
<td class="separation"><label for="debut">Début</label></td>
<td><!--<select id="debut" name="debut" class="select"></select>-->
<div>
{{ form_errors(form.start ) }}

{{ form_widget(form.start ) }}
</div>


</td>
</tr>
<tr>
<td class="separation"><label for="fin">Fin</label></td>
<td><!--<select id="fin" name="fin" class="select"></select>-->
<div>
{{ form_errors(form.end ) }}

{{ form_widget(form.end ) }}
</div>

</td>
</tr>

<tr>
<td class="separation"><label for="lieu">Lieu</label></td>
<td>

<div>
{{ form_errors(form.location) }}

{{ form_widget(form.location) }}
</div>

</td>
</tr>
<tr>
<td id="description" valign="top" class="separation"><label for="description">Description</label></td>
<td><textarea id="ikproj_groupebundle_eventsgroupe_description" name="ikproj_groupebundle_eventsgroupe[description]" rows="5" cols="40"></textarea>



</td>
</tr>
<tr>
<td colspan="2" align="center" id="button" valign="bottom"><input class="button" type="submit" value=""/></td>
</tr>
</table>
{{form_widget(form._token)}}
</form>
</td>
</tr>
</table>
</div>
</div>
</center>
</body>
</html>

这是表单类代码:

公共(public)函数buildForm(FormBuilderInterface $builder, array $options){ $ build 者

    ->add('title','text')
->add('start','datetime',array(
'input' => 'datetime',

'format' => 'dd/MM/yyyy H:i',
'minutes' => array(
0,
30
)
))
->add('end','datetime',array(
'input' => 'datetime',

'format' => 'dd/MM/yyyy H:i',
'minutes' => array(
0,
30
)
))

->add('location','text')
->add('description','text')

;

}

请重点关注这部分 JavaScript 代码(位于 html 表单代码的顶部),因为我尝试了这个,但没有成功:

<script>
function f1(){
alert($('#ikproj_groupebundle_eventsgroupe_start').val());
}
</script>

所以,我的问题是:执行此操作的正确代码是什么?

最佳答案

动态表单元素的 id 是在 Twig 中自动生成的,因此您的方法实际上不起作用。最好的方法是在模板中使用生成的id。可以通过 vars 属性轻松访问它:

alert( $('#{{ form.start.vars.id }}').val() );

关于javascript - 如何使用 Symfony2 中的警报消息通过 javascript 显示 html twig 表单字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25619339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com