【发布时间】:2014-09-01 15:02:59
【问题描述】:
我想知道如何在 Symfony2 中通过 javascript 访问 html 树枝表单字段值。解释如下:
这是我所拥有的表单的屏幕截图:
这是表单代码:
<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(){
if(form_widget(form.start)>form_widget(form.end)){
alert("no");
}
}
</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 function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->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(){
if(form_widget(form.start)>form_widget(form.end)){
alert("no");
}
}
</script>
所以,我的问题是:正确的代码是什么?
【问题讨论】:
标签: javascript forms symfony field twig