【问题标题】:Update the value of a dropdown base on a certain dropdown根据某个下拉列表更新下拉列表的值
【发布时间】:2016-02-29 01:59:52
【问题描述】:

我有 2 个下拉菜单。 一个下拉列表是国家。另一个下拉菜单是货币。 我想使用 jQuery 来控制用户选择的国家/地区的货币基础。 因此,每当用户更改国家/地区时,货币都会发生变化。

我的第一步是在用户选择时尝试获取所选值(国家名称)。

//Auto choose currency
    country = jQuery('#input_1_15 :selected').text();
    jQuery( "#input_1_15" ).change(function() {
      country = jQuery('#input_1_15 :selected').text();
    });
    alert(country);

但在用户选择时并没有打印出国家名称。
https://jsfiddle.net/uqvgsk6j/

【问题讨论】:

  • 问题是?
  • 抱歉,我更新了我的问题。
  • 将警报移动到 change 处理程序
  • 下拉菜单之间应该有关系。基于关系的值会发生变化。

标签: jquery


【解决方案1】:

如果你有相同的订单使用

$(function() {
    $("#input_1_15").on("change",function() {
      $("#input_1_16").get(0).selectedIndex=$(this).get(0).selectedIndex;
    });
});

【讨论】:

  • 谢谢。我有同样的订单。但法国、德国、意大利使用相同的货币。你的代码能用吗?
  • 仅当您有 3 欧元选项时。
【解决方案2】:

jQuery:

$(function(){
  $("#input_1_15" ).on("change", function() {       
      document.getElementById("input_1_16").selectedIndex = document.getElementById("input_1_15").selectedIndex;
  });
});

your fiddle updated

【讨论】:

    【解决方案3】:

    https://jsfiddle.net/refan/LtcjzoLp/3/

    如果可以在选项中添加数据属性:

    <option data-cur="JPY" value="Nhật Bản">Nhật Bản</option>
    

    jQuery("#input_1_15").change(function() {
    
      var selectedCurrency = $(this).find(":selected").data("cur");
      $("#input_1_16").val(selectedCurrency);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      相关资源
      最近更新 更多