填充对象列表中的选择

土豆24

我被要求'创建一个对象构造函数。它将具有属性名称和地址。它还将具有一个称为populate的方法,该方法会将地址附加到下面的选择中。

到目前为止,我已经创建了对象构造函数,但是我发现很难找到有关如何填充“选择”的信息。

我有:

function Addresses(name, address) {

    this.name = name;
    this.address = address;

}

var Add1 = new Addresses("Tom", "16 Rose Wood, Builth Wells, LD5 6AD");
var Add2 = new Addresses("Frank", "Ty Siriol, Brecon, LD4 6TE");
var Add3 = new Addresses("Ryan", "19 Garth Road, Builth Wells, LD2 3AR");
var Add4 = new Addresses("Beth", "10 Rock Road, Crossgates, LD1 6RP");
var Add5 = new Addresses("Darryl", "32 Oaklands Crescent, Builth Wells, LD2 9TD);

选择是:

        <select name="address" id="addr">
            <option>Select address</option>

        </select><br>

我将如何用这些对象填充此选择?我假设必须有一种无需硬编码的方法?

谢谢你。

******更新代码******

<!DOCTYPE html>
<html>
    <head>

        <title>Web Development with Javascript and AJAX</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/redmond/jquery-ui.css">
        <link rel="stylesheet" href="css/assign.css">
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>


        <script type="text/javascript">
/* 
 * Create an object constructor. It will have properties name and address. It will also have a method called populate which appends the address to the select below. 
 */

class Address {
  constructor(name, address) {
    // Constructor, as you already have.
    this.name = name;
    this.address = address;
  }
  populate(select) {
    // Parameter select is the id of the element you want to populate.
    var selectEl = document.getElementById(select);
    // Create option element.
    var option = document.createElement('option');
    // Fill it with information.
    option.text = this.name;
    option.value = this.name.toLowerCase();
    // Finally add it to the select element.
    selectEl.add(option);
  }
}

// Create some.
    var Add1 = new Address("Tom", "16 Rose Wood, Builth Wells, LD5 6AD");
    var Add2 = new Address("Frank", "Ty Siriol, Brecon, LD4 6TE");
    var Add3 = new Address("Ryan", "19 Garth Road, Builth Wells, LD2 3AR");
    var Add4 = new Address("Beth", "10 Rock Road, Crossgates, LD1 6RP");
    var Add5 = new Address("Darryl", "32 Oaklands Crescent, Builth Wells, LD2 9TD");

// Populate.
Add1.populate('select');
Add2.populate('select');
Add3.populate('select');
Add4.populate('select');
Add5.populate('select');



    </script>
    </head>

    <body>
        <header>
            <nav>
                <ul id="navigation">
                    <li>
                        <a href="food.html">Menu</a>
                    </li>
                    <li>
                        <a href="orderform.html">Order</a>
                    </li>


                </ul>
            </nav>
            <div style="clear: both"></div>
        </header>
        <div id="wrapper">
            <h1>Address</h1>


                <label for="address">Find your address:</label>

                <select name="address" id="addr">
                    <option>Select address</option>
                    <option id="Add1"></option>    
                    <option id="Add2"></option>    
                    <option id="Add3"></option>    
                    <option id="Add4"></option>    
                    <option id="Add5"></option>    
                </select><br>



                <br><br>
                <input type="submit" id="Submit" name="Submit" value="Submit" />

            </form>
        </div>

    </body>

</html>
法比安·舒尔茨(Fabian Schultz)

这是使用ES6'的示例Class

class Address {
  constructor(name, address) {
    // Constructor, as you already have.
    this.name = name;
    this.address = address;
  }
  populate(select) {
    // Parameter select is the id of the element you want to populate.
    var selectEl = document.getElementById(select);
    // Create option element.
    var option = document.createElement('option');
    // Fill it with information.
    option.text = this.name;
    option.value = this.name.toLowerCase();
    // Finally add it to the select element.
    selectEl.add(option);
  }
}

// Create some.
var Add1 = new Address('Tom', '16 Rose Wood, Builth Wells, LD5 6AD');
var Add2 = new Address('Frank', 'Ty Siriol, Brecon, LD4 6TE');

// Populate.
window.onload = () => {
  Add1.populate('addr');
  Add2.populate('addr');
}
<select id="addr"></div>

使用ES5:

function Address(name, address) {
  this.name = name;
  this.address = address;
  this.populate = function(select) {
    // Parameter select is the id of the element you want to populate.
    var selectEl = document.getElementById(select);
    // Create option element.
    var option = document.createElement('option');
    // Fill it with information.
    option.text = this.name;
    option.value = this.name.toLowerCase();
    // Finally add it to the select element.
    selectEl.add(option);
  }
}

// Create some.
var Add1 = new Address('Tom', '16 Rose Wood, Builth Wells, LD5 6AD');
var Add2 = new Address('Frank', 'Ty Siriol, Brecon, LD4 6TE');

// Populate.
window.onload = function() {
  Add1.populate('addr');
  Add2.populate('addr');
}
<select id="addr"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

选择嵌套的JSON对象并填充下拉列表

来自分类Dev

如何使用angular随对象一起从对象中填充选择下拉列表。

来自分类Dev

基于ASP.NET MVC中的复选框选择填充模型的嵌套列表对象

来自分类Dev

从选择中填充列表框,python

来自分类Dev

在angularjs中填充下拉列表选择

来自分类Dev

无法填充模型中的选择列表

来自分类Dev

根据Polymer中的列表选择填充内容

来自分类Dev

使用选择列表中的内容填充Div

来自分类Dev

在Android中动态填充对象的列表视图

来自分类Dev

在linq to sql中填充对象列表

来自分类Dev

如何从对象列表项中删除填充?

来自分类Dev

尝试填充并返回对象的HashMap中的列表

来自分类Dev

尝试填充并返回对象的HashMap中的列表

来自分类Dev

从对象列表填充对象

来自分类Dev

GORM:将列表填充到选择下拉列表中

来自分类Dev

填充列表 <> 不是选择列表

来自分类Dev

选择列表中的所有对象

来自分类Dev

使用JSON数组中的键填充选择下拉列表

来自分类Dev

如何在Bootstrap中预填充“选择列表”的值?

来自分类Dev

在控制器中获取类别列表以填充选择

来自分类Dev

有条件地在Angular中填充选择列表

来自分类Dev

Ractive填充选择列表,json中为空值

来自分类Dev

如何从列表框选择中填充组合框?

来自分类Dev

从数据库中填充剃刀选择列表

来自分类Dev

如何舒适地用Groovy中的新对象填充列表?

来自分类Dev

根据选择填充下拉列表

来自分类常见问题

选择列表的内部对象

来自分类Dev

选择列表的内部对象

来自分类Dev

jQuery函数-使用其他列表中的下拉选择填充的简单下拉选择