2011-11-18 57 views
0

我有一个下拉列表onchange事件。当用户改变下拉框时,Ajax调用并显示结果。这一切工作正常。下降与onchange事件

我在模板中有以下列表。

Apple 
Pineapple 

当用户更改下拉值时。然后结果合并

Orange 
Graps 

Apple 
Pineapple 

但输出应该是这样的。

Orange 
Graps 

我不知道该怎么做。请帮帮我。这是我的base。这是我的movie_list模板。这是我的movie_sort模板。这是我的view。谢谢:-)

更新:这里是ajax代码。

function sortMovie(str) 
{ 
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 

    } 
    } 
xmlhttp.open("GET","/moviesort/?q="+str,true); 
xmlhttp.send(); 

} 

下拉

  <select name="category" onchange="sortMovie(this.value)"> 
       <option value="">Choose</option> 
       {% for category in categories %} 
       <option value="{{ category.id}}">{{category.name}}</option> 
       {% endfor %}      
       </select> 
+0

试试这个回答:http://stackoverflow.com/questions/5442104/what-is-the-best-jquery-multiselect-picker-for-hierarchical-data – PhoebeB

回答

0

如果您正在使用jQuery来调用AJAX,我建议你使用.empty()的.change()函数后直。

我不知道你的样子是什么,虽然它应该是这样的。

$('#id_category').change(function() { 
    /* Set your string to nothing */ 
    var str = ""; 
    /* once your dropdown is changed, it performs a look and gets the new data */ 
    $("#id_category option:selected").each(function() { 
     /* Before inserting the new data, empty out all the old items */ 
     $('#id_sub_category').empty(); 
     str += $(this).val(); 
     $.get('{% url gear_category %}', { category: str }, function(data){ 
      $(data).appendTo("#id_sub_category"); 
     }); 
    }); 
}); 

希望这会有所帮助。

+0

我使用简单的ajax函数而不是jquery。 – Kulbir

0

我同意ApPel对此。

但是,因为你没有使用jQuery ...

你要么通过各个选项必须循环在选择并删除你去,或者我想,如果纯JavaScript支持你也许可以模仿jQuery代码,但使用JavaScript语法。

无论哪种方式,您必须在添加新值之前删除旧值。那就是问题所在。