简体   繁体   中英

dynamic item selection of product and price

I'm having a list of items in an array with its prices. The list of items must be displayed in a dropdown . When one is selected, I want its corresponding price to be placed into a text box called price .

JS:

function PopulateDropDownList() {
  var products = [
    { productId: 1, name: "Fanta", price: "4" },
    { productId: 2, name: "Coke", price: "2" },
    { productId: 3, name: "Sprite", price: "8" },
    { productId: 4, name: "Malta Guniness", price: "10" }
  ];
}

HTML:

<body onload="PopulateDropDownList()">
  <hr />
  <select id="productsDropDown">
  </select>

  <input type="text" name="price" value="">
</body>

I think you are trying something like below-posted code.

 function myFunction(e) { document.getElementById("price").value = e.target.value }
 <hr /> <select id="productsDropDown" name="productsDropDown" onchange="myFunction(event)"> <option value="4">productId: 1</option> <option value="2">productId: 2</option> <option value="8">productId: 3</option> <option value="10">productId: 4</option> </select> <input type="text" size="30" name="price" id="price" />

 var demo = angular.module('demo', []); function MyCtrl ($scope) { $scope.myGroups = [ {label:'Admin', value:1}, {label:'Users', value:2}, {label:'Public', value:3} ]; $scope.tellUs = function() { console.log("the selected group is - " + $scope.group); }; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.6/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div ng-app='demo' ng-controller='MyCtrl'> <p>You've just selected {{group}}.</p> <form> <select ng-model="group" ng-options="o.value as o.label for o in myGroups" ng-change="tellUs()"/> </form> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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