简体   繁体   English

动态创建选择框并赋值

[英]Dynamically create Select boxes and assign values

Inside angular app.js I am dynamically placing multiple select box in a form with a unique id.在 angular app.js 中,我在具有唯一 ID 的表单中动态放置多个选择框。 How can is get the values selected on those select boxes inside the controller.如何获取在控制器内的那些选择框中选择的值。

Added a variable添加了一个变量

$scope.sibling_type = {};

Javascript code to load the select box.用于加载选择框的 Javascript 代码。

$scope.linkSiblingFinish = function(sibling){
   var str_siblingtype = "<div class='col-sm-2' style='margin-bottom:15px'>" +
     "<select ng-model='sibling_type[" + sibling.id + "]' class='form-control' id='sibling_type_"+sibling.id+"'>" +
       "<option class='ng-binding ng-scope' value='1'>Sister->Brother</option>" +
       "<option class='ng-binding ng-scope' value='2'>Brother->Sister</option>" +
       "<option class='ng-binding ng-scope' value='3'>Sister->Sister</option>" +
       "<option class='ng-binding ng-scope' value='4'>Brother->Brother</option>" +
     "</select></div>";

    document.getElementById('div_siblings').innerHTML = str_siblingtype;
}

The above script will be called on a button click from where the variable 'sibling' will be loaded with different 'id' every time.上面的脚本将在单击按钮时调用,变量 'sibling' 将每次加载不同的 'id'。 Assume it has been called for 2 times with the id as '23' and '24'.假设它被调用了 2 次,id 为“23”和“24”。 There will be 2 select boxes as 'sibling_type_23' and 'sibling_type_24'.将有 2 个选择框作为“sibling_type_23”和“sibling_type_24”。

On submit button在提交按钮上

$scope.saveSibling = function(data){
    dataFactory.httpRequest('index.php/students/sibling/'+$scope.form.id,'POST',{},$scope.form).then(function(data) {
    });
}

How do I bind/assign the option values so that when the form is submitted I can get the selected options on those select boxes inside Laravel controller.我如何绑定/分配选项值,以便在提交表单时我可以在 Laravel 控制器内的那些选择框中获取所选选项。

You can use a different approach for this:您可以为此使用不同的方法:

  1. Create a new 'sibling_type' scope inside your controller.在您的控制器中创建一个新的“sibling_type”范围。

     $scope.sibling_type = {};
  2. Next, update the ng-model for all dynamic selects inside linkSiblingFinish like:接下来,更新linkSiblingFinish所有动态选择的ng-model ,例如:

     "<select ng-model='sibling_type[" + sibling.id + "]' class='form-control' id='sibling_type_"+sibling.id+"'>" +
  3. Now, when the form is submitted you can get the selected options on those select boxes.现在,提交表单后,您可以在这些选择框中获得所选选项。 like:喜欢:

     var sibling_type_23 = $scope.sibling_type['23']; var sibling_type_24 = $scope.sibling_type['24'];

DEMO:演示:

 const app = angular.module('myApp', []); app.controller('AppCtrl', function ($scope) { $scope.siblings = [{id: 23,name: 'Sibling 23'},{id: 24,name: 'Sibling 24'}]; $scope.sibling_type = {}; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="AppCtrl"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr ng-repeat="sibling in siblings"> <td>{{sibling.name}}:</td> <td><input type="text" ng-model="sibling_type[sibling.id]" placeholder='Type here..' /></td> </tr> </table> <pre>{{sibling_type | json }}</pre> </div> </div>

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

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