gpt4 book ai didi

javascript - Symfony 1.4 与 AJAX jQuery。如何改进包含一个依赖于另一个选择框的选择框的 AJAX 功能?

转载 作者:行者123 更新时间:2023-11-28 08:50:16 24 4
gpt4 key购买 nike

我使用 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 函数。代码如下:

<?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) ?>
&nbsp;<a href="<?php echo url_for('conflictos/index') ?>"><button class="btn btn-success" type="button">Listado</button></a>
<?php if (!$form->getObject()->isNew()): ?>
&nbsp;<?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 对应的字段显示为空。

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

最佳答案

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

<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>

现在加载文档时会执行相同的函数。现在我在这里有了更好的答案:Symfony 1.4: How I can retrieve the selected value with AJAX's function in select dependent?

关于javascript - Symfony 1.4 与 AJAX jQuery。如何改进包含一个依赖于另一个选择框的选择框的 AJAX 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19236402/

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