簡體   English   中英

如何使用Symfony2中的警報消息通過javascript顯示到html樹枝表單字段值?

[英]how to display to a html twig form field value through javascript using an alert message in Symfony2?

我想知道如何使用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(){
                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>

這是表單類代碼:

公共功能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(){
                alert($('#ikproj_groupebundle_eventsgroupe_start').val());
            }
        </script>

因此,我的問題是:什么是正確的代碼?

動態表單元素的id是在Twig中自動生成的,因此您的方法將無法真正起作用。 最好的方法是利用模板中生成的id 可以通過vars屬性輕松訪問它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM