2012-02-28 145 views
4

在你-1之前,这是一个如此简单的问题,因为我花了一段时间搜索并找不到它。我有什么是5个选择框。每个结果取决于之前选择的内容。没有提交按钮。我只需要根据在上一个框中选择的内容进行更改,到目前为止我已经拥有了该功能。哦,结果正在从数据库中提取。问题在于,当您在选项1中选择一个选项时,会出现框2选项,但当您再次选择框1中的选项时,它们的选项将叠加在其他选项上。当它再次改变时,我需要他们清除。我会张贴我拥有的js,如果你还需要其他的东西,就问问。使用javascript更改选择选项

p.s.即时通讯使用php-mysql的选项。 ps.s.s这个网站必须在明天完成,所以即使有点急。请不要浪费时间告诉我这是一个愚蠢的问题,或者不是。

在此先感谢您的帮助。

$(function() { //When the page is finished loading run this code 
    $('#country').change(function() { 
     $.ajax({ 
      url: "<!--Base url taken out-->/discovery/aLoad", 
      success: function (data) { 
       eval(data); 
      }, 
      error: function() { 
       alert('failed'); 
      }, 
      data: { 
       country: getSelectValues('#country') 
      }, 
      type: 'POST' 
     }); 
    }); 
}); 

    function changeSearchBar (newBar) { 
    //remove the crap 
    function ClearOptions(country) 
    { 
    document.getElementById('country').options.length = 0; 
    } 

    for (var i = 0; i <= newBar.length; i++) { 
     newBar[i].id = newBar[i][0]; 
     newBar[i].name = newBar[i][1]; 
     $('#group').append(
      $('<option></option>').attr('value', newBar[i].id).text(newBar[i].name) 
     ); 
    } 
    } 

    function getSelectValues (sId) { 
    var str = ""; 

    $(sId +" option:selected").each(function() { 
     str += $(this).val() + ","; 
    }); 

    return str.replace(/.$/g, ''); 
} 
+0

您的ClearOptions功能看起来很不错,但我没有看到它在哪里调用它。它被定义为changeSearchBar,但从未被调用。我想如果你在for循环之前调用它,你应该没问题。 – Zoidberg 2012-02-28 02:49:11

回答

1

从我从你长的帖子理解你只需要开始追加从AJAX请求新的选项之前删除所有select孩子的。

尝试这样的事情你for循环之前:

$('#group').html('');​ 

演示:http://jsfiddle.net/57TYu/2/

+0

这就像你的天才或什么! :D是的,我知道这是一个很长的帖子。不知道如何澄清它。但是,这是有效的。非常感谢你。 – 2012-02-28 02:53:27

+0

等待,对不起,这摆脱了选择多个选项 – 2012-02-28 02:54:16

+0

我需要能够选择多个并具有与这两个返回相关联的所有第二个选项框选项。有任何想法吗? – 2012-02-28 02:54:55

0

在这里,你走了,我觉得这基本上就是你正在尝试做的:

How do you remove all the options of a select box and then add one option and select it with jQuery?

基本上,找到'选项'标签内t他选择并移除它们。

$('select').find('option').remove() 

将清除所有的选择框。

这将追加数据,这是假设在自己的位置的选项:

$('select').find('option').remove().end().append(data); 

我认为这是你在找什么。