【问题标题】:Jquery Populate dropdown from otherJquery从其他填充下拉列表
【发布时间】:2013-05-20 19:34:00
【问题描述】:

我正在从另一个下拉值填充下拉列表。第一次一切都很好,但是当我再次更改第一个下拉列表时,以前的值仍然显示在第二个下拉列表中。以下代码我用来填充第二个下拉列表!

<select id="NextSession" name="NextSession">
<option value="1">Coffee</option>
<option value="2">Tea</option>
</select>

<select id="NextSectionId" name="NextSectionId">
<option><option>
</select>

$("#NextSession").blur(function() {
$("#NextSectionId").load("/GetData/" + $("#NextSession").val());
});

这是我的 php 代码:

$UniqueId=$_GET['UniqueId'];
$query2="select ClassName,SectionName,SectionId from class,section where class.ClassId=section.ClassId and class.ClassStatus='Active' and section.SectionStatus='Active' and class.Session='$UniqueId' order by ClassName";
$check2=mysql_query($query2);
while($row2=mysql_fetch_array($check2))
{
$SelectClassName=$row2['ClassName'];
$SelectSectionName=$row2['SectionName'];
$SelectSectionId=$row2['SectionId'];
echo "<option value=\"$SelectSectionId\">$SelectClassName $SelectSectionName</option>";
}

请告诉我是什么问题!!

这是一个活生生的例子!!

myraipur.com/test/test.php

在第一个下拉列表中选择任何选项,从第二个中选择...然后选择第一个,第二个下拉列表保持原样!!!

【问题讨论】:

  • 试试这个:$("#NextSession").change(function () { $("#NextSectionId").load("/GetData/" + this.value); });
  • 仍然面临同样的问题!!!

标签: jquery


【解决方案1】:

尝试将 bur 回调更改为 change 并在新请求清除所有旧值之前。

类似这样的:

$("#NextSession").change(function() {
  $("#NextSectionId").empty();
  $("#NextSectionId").load("/GetData/" + $("#NextSession").val());
});

一些外部链接: jQuery.load jQuery.empty

【讨论】:

  • 我试过html,empty,clear,reset所有功能!!!但他们都没有工作!!!
  • 是的,每次我更改下拉列表时都会从服务器获取准确的数据...但是之前的结果也包含在新结果中!!!
  • 试试这个:$("#NextSectionId li").remove();insted of $("#NextSectionId").empty();
  • 你能举个例子让我们看看发生了什么吗?
  • $("#NextSectionId").load("/GetData/" + $("#NextSession").val(), function(response, status, xhr) { alert(response); alert(status); alert(xhr); });
【解决方案2】:

在加载内容之前尝试清除第二个选择

$("#NextSession").blur(function() {
  $("#NextSectionId").html(''); //emptying select field
  $("#NextSectionId").load("/GetData/" + $("#NextSession").val());
});

【讨论】:

  • 检查数据是否仅返回所选值的内容,即 1 或 2
  • 问题必须来自服务器端。尝试对返回的数组执行 print_r。
  • 每次我更改下拉列表时都从服务器获取准确的数据...但是以前的结果也包含在新结果中!!!
  • 当您说“之前的结果也包含在新结果中”时,您是在说选择框内容(UI)还是返回的数组本身具有之前的结果?
  • 我说的是选择框内容!!!服务器的结果每次都是正确的...但是选择框具有先前选择的值!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 2013-04-16
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多