Javascript 如何使用jQuery动态更改固定的选定框值

Javascript 如何使用jQuery动态更改固定的选定框值,javascript,jquery,html,jquery-ui,Javascript,Jquery,Html,Jquery Ui,我有3个选择框。每个选择框值必须动态更改 我想根据第一个选择框更改第二个和第三个选择框的值。 用jQuery简单的方法修复 Commerce->English Arts->Gujarati science->French Commerce->IP Arts->Computer Science->java 请在这里查看html 这是样品2盒,我想要3盒 JQUERY代码在这里 $("#select1").change(function() { if

我有3个选择框。每个选择框值必须动态更改

我想根据第一个选择框更改第二个和第三个选择框的值。 用jQuery简单的方法修复

Commerce->English
Arts->Gujarati
science->French

Commerce->IP    
Arts->Computer
Science->java
请在这里查看html

这是样品2盒,我想要3盒

JQUERY代码在这里

$("#select1").change(function() { 
if($(this).data('options') == undefined){
    /*Taking an array of all options-2 and kind of embedding it on the select1*/
    $(this).data('options',$('#select2 option').clone());
    } 
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});

我不敢相信你想要的是这么简单:

var secondSelect = $("select[name='2ndbox']");
var thirdSelect = $("select[name='3rdbox']");
var secondOptions = secondSelect.children('option');
var thirdOptions = thirdSelect.children('option');

$("select[name='1stbox']").change(function(){     
  var selectedIndex = this.selectedIndex;
  secondOptions.filter(function(i){ return i == selectedIndex})
               .appendTo(secondSelect.empty());
  thirdOptions.filter(function(i){ return i == selectedIndex})
              .appendTo(thirdSelect.empty());
}).change();
secondOptions.eq(selectedIndex).appendTo(secondSelect.empty());
请注意,使用filter只是实现它的一种通用方法,在您的特定情况下,看起来我们可以这样使用.eq方法:

var secondSelect = $("select[name='2ndbox']");
var thirdSelect = $("select[name='3rdbox']");
var secondOptions = secondSelect.children('option');
var thirdOptions = thirdSelect.children('option');

$("select[name='1stbox']").change(function(){     
  var selectedIndex = this.selectedIndex;
  secondOptions.filter(function(i){ return i == selectedIndex})
               .appendTo(secondSelect.empty());
  thirdOptions.filter(function(i){ return i == selectedIndex})
              .appendTo(thirdSelect.empty());
}).change();
secondOptions.eq(selectedIndex).appendTo(secondSelect.empty());

依赖于第一个选择框它是如何依赖的?如果我从第一个选择框中选择commerce,则意味着动态更改。第二个选择框和第三个选择框的值必须更改为动态。您忘记发布您尝试过但不起作用的JavaScript。@j08691-我不这么认为。这就是堆栈溢出众包的用武之地=/@vivek-从这里开始注意:如果我仍然误解了您的问题,请留下一些评论,我会解决。非常感谢您的时间,但我只想显示相关值,如果用户选择commerce,则只显示英文和IP@vivek你的意思是英语应该显示在第二个选项上吗,IP应显示在第三个选项上,第二个和第三个选项中的两个选项不应有任何其他选项可供选择?这确实很奇怪,因为这不是选择元素的目的,它应该有多个选项。是的,我想更改两个选择框的值,就像这样,用户从第一个框中选择商业,然后第二个框将只显示英语,第三个框将只显示IP Commerce->english Commerce->IPyou救了我的命这很好:工作很好:谢谢你的回答和宝贵的时间给我的心