簡體   English   中英

如何從HTML輸入中獲取多個值?

[英]How to get multiple values from HTML inputs?

我正在關注流星Tuturial( https://www.meteor.com/tutorials/blaze/creating-an-app ),這是HTML和JavaScript代碼的一部分:

的HTML

 <body>
  <div class="container">`enter code here`
    <header>
      <h1>Todo List</h1>

      <form class="new-task">
        <input type="text" name="text" placeholder="Type to add new tasks" />
      </form>

    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

的JavaScript

    Template.body.events({
    "submit .new-task": function (event) {
      // Prevent default browser form submit
      event.preventDefault();

      // Get value from form element
      var text = event.target.text.value;

      // Insert a task into the collection
      Tasks.insert({
        text: text,
        createdAt: new Date() // current time
      });

      // Clear form
      event.target.text.value = "";
    }
  });

我想知道如何在HTML表單中添加另一個輸入並在Java腳本中訪問該另一個輸入。

例如:

<input type="text" name="city" placeholder="Type to add city" />

如果將示例輸入插入HTML,則新的javascript應該是:

Template.body.events({
"submit .new-task": function (event) {
  // Prevent default browser form submit
  event.preventDefault();

  // Get value from form element
  var text = event.target.text.value;
  var city = event.target.city.value;

  // Insert a task into the collection
  Tasks.insert({
    text: text,
    createdAt: new Date() // current time
  });
  Tasks.insert({
    text: city,
    createdAt: new Date() // current time
  });

  // Clear form
  event.target.text.value = "";
  event.target.city.value="";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM