使用AJAX jQuery的Symfony 1.4。 如何改进包含取决于另一个选择框的选择框的AJAXfunction?

我使用Symfony 1.4。 我有三个相互关联的表,如下所示。

表格1:

Conflictos1: connection: doctrine tableName: conflictos_1 columns: id: type: integer(4) fixed: false unsigned: false primary: true autoincrement: true id_sector_actividad: type: integer(4) fixed: false unsigned: false primary: false notnull: true autoincrement: false id_subsector_actividad: type: integer(4) fixed: false unsigned: false primary: false notnull: false autoincrement: false relations: SectorActividadCiuTa7: local: id_sector_actividad foreign: id type: one SubsectorActividadTa8: local: id_subsector_actividad foreign: id type: one 

表2和3:

 SectorActividadCiuTa7: connection: doctrine tableName: sector_actividad_ciu_ta7 columns: id: type: integer(4) fixed: false unsigned: false primary: true autoincrement: true sector_actividad: type: string(255) fixed: false unsigned: false primary: false notnull: true autoincrement: false relations: Conflictos1: local: id foreign: id_sector_actividad type: many SubsectorActividadTa8: local: id foreign: id_sector type: many SubsectorActividadTa8: connection: doctrine tableName: subsector_actividad_ta8 columns: id: type: integer(4) fixed: false unsigned: false primary: true autoincrement: true id_sector: type: integer(4) fixed: false unsigned: false primary: false notnull: true autoincrement: false descripcion: type: string(255) fixed: false unsigned: false primary: false notnull: true autoincrement: false relations: SectorActividadCiuTa7: local: id_sector foreign: id type: one Conflictos1: local: id foreign: id_subsector_actividad type: many 

我用Symfony生成了一个名为“conflictos”的模块。 在这个模块中,我有一个部分_form.php。这个部分包含一个AJAX函数。 这里的代码:

      $(document).ready(function() { $("#conflictos1_id_sector_actividad").change(function() { var id_sub = $(this).val(); if(id_sub != '') { $.ajax ({ type: "POST", url: ''+ '?id=' + id_sub, cache: false, data: "id_sub="+ id_sub, success: function(data) { $("#conflictos1_id_subsector_actividad").html(data); // but it does not select the value of dropdown list. } }); } else { $("#conflictos1_id_subsector_actividad").html("-- No se ha seleccionado subsector --"); } return false; }); });  <form action="getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" isMultipart() and print 'enctype="multipart/form-data" ' ?>> getObject()->isNew()): ?>    renderGlobalErrors() ?> 
renderHiddenFields(false) ?>  <a href="https://stackoverflow.com/questions/19236402/symfony-1-4-with-ajax-jquery-how-can-i-improve-the-ajaxs-function-that-contain/"> getObject()->isNew()): ?>  <?php echo link_to('', 'conflictos/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => '¿Está seguro?')) ?> <!---->
<?php echo "Sector Actividad (*)
" ?> renderError() ?>
<?php echo "Subsector Actividad
" ?> renderError() ?> Seleccione sub-sector

添加新记录时,AJAX函数工作正常,但是当我想编辑记录时,select id_subsector_actividad对应的字段显示为空。

我的问题是:当我用Symfony调用函数executeEdit时,我应该在AJAX函数中更改,显示字段值“id_subsector_actividad”?

我解决了这个问题。 该函数应如下所示:

  

现在,在加载文档时执行相同的function。 现在我在这里得到了更好的答案: Symfony 1.4:我如何使用选择依赖的AJAX函数检索所选值?