Symfony 1.4和AJAX jQuery.如何改善AJAX的功能,该功能包含一个依赖于另一个选择框的选择框? [英] Symfony 1.4 with AJAX jQuery. How can I improve the AJAX's function that contains a select box which depends on another select box?
问题描述
我使用Symfony 1.4.我有三个相互关联的表,如下所示.
I use Symfony 1.4. I have three tables related to one another as shown below.
表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函数.这里的代码:
I have generated with Symfony a module named "conflictos". In this module I have a partial _form.php.This partial contains an AJAX function. Here the code:
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<?php use_helper('Date') ?>
<!--Aquí el javascript para select dependientes-->
<script type="text/javascript">
$(document).ready(function()
{
$("#conflictos1_id_sector_actividad").change(function()
{
var id_sub = $(this).val();
if(id_sub != '')
{
$.ajax
({
type: "POST",
url: '<?php echo url_for('conflictos/subsector'); ?>'+ '?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("<option value=''>-- No se ha seleccionado subsector --</option>");
}
return false;
});
});
</script>
<form action="<?php echo url_for('conflictos/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
<?php if (!$form->getObject()->isNew()): ?>
<input type="hidden" name="sf_method" value="put" />
<?php endif; ?>
<table border="0" width="96%">
<tfoot>
<tr>
<td align="center" colspan="4">
<?php echo $form->renderHiddenFields(false) ?>
<a href="<?php echo url_for('conflictos/index') ?>"><button class="btn btn-success" type="button">Listado</button></a>
<?php if (!$form->getObject()->isNew()): ?>
<?php echo link_to('<button class="btn btn-danger" type="button">Borrar</button>', 'conflictos/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => '¿Está seguro?')) ?>
<?php endif; ?>
<!--<input type="submit" value="Save" />-->
<button class="btn btn-primary" type="submit">Grabar</button>
</td>
</tr>
</tfoot>
<tbody>
<?php echo $form->renderGlobalErrors() ?>
<tr>
<td width="24%"><?php echo "<b>Sector Actividad <font color='red'> (*)</b></font><br>" ?>
<?php echo $form['id_sector_actividad']->renderError() ?>
<?php echo $form['id_sector_actividad'] ?>
</td>
<td width="24%"><?php echo "<b>Subsector Actividad</b><br> " ?>
<?php echo $form['id_subsector_actividad']->renderError() ?>
<select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad">
<option value="" selected="selected">Seleccione sub-sector</option>
</td>
</tbody>
</table>
</form>
当我添加新记录时,AJAX函数可以正常工作,但是当我要编辑记录时,与select id_subsector_actividad对应的字段显示为空.
The AJAX function works fine when I add a new record, but when I want to edit a record, the field corresponding to select id_subsector_actividad appears empty.
我的问题是:当我用Symfony调用函数executeEdit时,应该如何更改AJAX函数以显示字段值"id_subsector_actividad"?
My question is: What should I change in the AJAX function, to display the field value "id_subsector_actividad", when I call the function executeEdit with Symfony?
推荐答案
我解决了这个问题.该函数应如下所示:
I solved the problem. The function should look like:
<script type="text/javascript">
$(document).ready(function()
{
var sector = document.getElementById("conflictos1_id_sector_actividad");
var id_sub = sector.options[sector.selectedIndex].value;
{
$.ajax
({
type: "POST",
url: '<?php echo url_for('conflictos/subsector'); ?>'+ '?id=' + id_sub,
cache: false,
data: "id_sub="+ id_sub,
success: function(data)
{
// alert(data);// its show my returned value
$("#conflictos1_id_subsector_actividad").html(data);// but it does not select the value of dropdown list.
}
});
}
$("#conflictos1_id_sector_actividad").change(function()
{
var id_sub = $(this).val();
if(id_sub != '')
{
$.ajax
({
type: "POST",
url: '<?php echo url_for('conflictos/subsector'); ?>'+ '?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("<option value=''>-- No se ha seleccionado subsector --</option>");
}
return false;
});
});
</script>
现在在加载文档时执行相同的功能. 现在,我在这里有了更好的答案:
The same function is now executed when the document is loaded. Now I've the better answer here: Symfony 1.4: How I can retrieve the selected value with AJAX's function in select dependent?
这篇关于Symfony 1.4和AJAX jQuery.如何改善AJAX的功能,该功能包含一个依赖于另一个选择框的选择框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!