简体   繁体   English

使用两个下拉列表的值填充文本框

[英]Populate text box using values of two dropdown

I want to populate a text box with pre-defined values.我想用预定义的值填充文本框。 This text box is read-only.此文本框是只读的。 User can select from 2 inter-dependent dropdown boxes.用户可以从 2 个相互依赖的下拉框中进行选择。 All this is part of a form in a bootstrap project.所有这些都是引导项目中表单的一部分。

Taking cue from this , I am able to populate second dropdown by selecting first.从以提示这个,我可以先选择填充第二个下拉。 However, cannot combine the functionality of both and link them to the text box.但是,不能结合两者的功能并将它们链接到文本框。

Code:代码:

<select id="cat">
<option val="Laptops">Laptops</option>
<option val="Phones">Phones</option>
<option val="Tablets">Tablets</option>
<option val="PC">PC</option>
</select>
<select id="item"></select>
<input id='price' type='text' readonly/>

javascript: javascript:

Laptops=new Array("HP","Dell","Apple","Lenovo");
Phones=new Array('Samsung','Nokia','iPhone');
Tablets=new Array('Apple','Lenovo','Asus','Acer','HP');
PC=new Array('IBM','Macintosh','Dell');

populateSelect();

$(function() {

  $('#cat').change(function(){
    populateSelect();
  });
});

function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
  eval(cat).forEach(function(t) { 
        $('#item').append('<option>'+t+'</option>');
    });
}

I have about 5 and 4 values in dropdowns 1 and 2 respectively.我在下拉列表 1 和 2 中分别有大约 5 个和 4 个值。 Not using database as of now.目前没有使用数据库。

How can we add price in text box when user selects from any of the dropdown?当用户从任何下拉列表中选择时,我们如何在文本框中添加价格?

As you want to show the price of the item, i don't see it in the curren object structure.由于您想显示商品的价格,我在当前对象结构中看不到它。 So i've modified the object structure a bit.所以我稍微修改了对象结构。

Also i prefer using [] for array creation when compared to new Array.此外,与新数组相比,我更喜欢使用[]来创建数组。

Similarly i don't prefer using eval , instead as the variables are globally available they are properties of window object.同样,我不喜欢使用eval ,而是因为变量是全局可用的,它们是 window 对象的属性。 So you can use window[property] to get that particular array.因此,您可以使用window[property]来获取该特定数组。 But instead I would prefer creating a new object and attaching all properties to that object.但相反,我更喜欢创建一个新对象并将所有属性附加到该对象。 So that you can avoid polluting the global namespace.这样就可以避免污染全局命名空间。

Laptops=new Array({name : "HP", price : 200},{name : "Dell", price : 400},{name : "Apple", price : 400},{name : "Lenovo", price : 200});
Phones=new Array({name : "Samsung", price : 200},{name : "Nokia", price : 400},{name : "Apple", price : 400});
Tablets=new Array({name : "Apple", price : 200},{name : "Acer", price : 400},{name : "Lenovo", price : 400},{name : "Dell", price : 100});
PC=new Array({name : "Macnitosh", price : 200},{name : "Dell", price : 100});


$(function() {
function populateSelect(){
  var value = $("#cat").val();
  if(value) {
    console.log("value"+window[value]);
    $("#item").html("");
    $.each(window[value], function(index, val) {
      $("#item").append("<option value='"+val.price+"'>"+val.name+"</option>");
    });
  }
}
  $('#cat').change(function(){
    populateSelect();
  });
  populateSelect();

  $("body").on("change", "#item", function() {
    $("#price").val($(this).val());
  });
});

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM