简体   繁体   English

如何从Meteor中的动态创建输入中获取价值?

[英]How to get value from dynamic create input in Meteor?

I have created a custom form and added some jquery for dynamic inputs, but I can't get values from these generated inputs ( Elements, Link, Image ). 我已经创建了一个自定义表单并为动态输入添加了一些jquery,但是我无法从这些生成的输入( Elements,Link,Image )中获取值。 Here are my form and code. 这是我的表格和代码。

在此处输入图片说明 Template This is my template for this form 模板这是我此表格的模板

<form id="workForm" class="workAdd">
<label>Title</label>
<input type="text" name="title" id="title" class="form-control">
<label>Live Link</label>
<input type="text" name="llink" id="llink" class="form-control">
<label>Elements Used In This Project</label>
<div class="row" id="eleContainer">
    <div class="eleRow">
        <div class="col s6 m6 l6">
            <label>Element</label>
            <input type="text" name="elements[]" id="elementsid[]" class="form-control">
        </div>
        <div class="col s6 m6 l6">
            <label>Link</label>
            <input type="text" name="elinks[]" id="elinksid[]" class="form-control" >
        </div>
    </div>
</div><!--row-->
<div class="row">
    <div class="col s6 m12 center">
        <a class="btn-floating waves-effect waves-light" id="addElements"><i class="fa fa-plus"></i></a>
    </div>
</div>
<div class="row" id="imgContainer">
    <div class="imgRow">
        <label>Image</label>
        <input type="text" name="image[]" id="imageid[]" class="form-control" >
    </div>
</div>
<div class="row">
    <div class="col s6 m12 center">
        <a class="btn-floating waves-effect waves-light" id="addImages"><i class="fa fa-plus"></i></a>
    </div>
</div>
<label>Detail</label>
<textarea id="detail" class="materialize-textarea" name="detail" data-length="120"></textarea>

<button type="submit" id="work-submit" thisId="" class="btn btn-primary">Submit</button>

Meteor And this is my submit event I have created for this form. 流星这是我为此表单创建的提交事件。

Template.addworkTemp.events({
"submit .workAdd": function(event) {

    var title = event.target.title.value;
    var llink = event.target.llink.value;
    var elements = event.target.elements.value;
    var elinks = event.target.elinks.value;
    var images = event.target.images.value;
    var detail = event.target.detail.value;
    return false;

There's a good tutorial on meteor forms that might be useful to you. 关于流星形式的很好的教程可能对您有用。 This tutorial is based on blaze which is what you're using. 本教程基于您正在使用的烈火。

Firstly you can attach your handler to the form instead of to the workform class. 首先,您可以将处理程序附加到form而不是工作workform类。

Template.addWorkTemp.events({
  'submit form'(event) {
  ...
  }
});

Then you should prevent the default form action: 然后,您应该阻止默认的表单操作:

Template.addWorkTemp.events({
  'submit form'(event) {
    event.preventDefault();
    ...
  }
});

The event object has a target property which has properties for every field based on the name attached to them in the html. event对象具有target属性,该属性具有基于html中附加给它们的名称的每个字段的属性。

Template.addWorkTemp.events({
  'submit form'(event) {
    event.preventDefault();
    console.log(event.target.llink.value);
    ...
  }
});

I have no idea why you're appending [] to the field names. 我不知道为什么要在字段名称后附加[] I'm not sure those are valid in that context. 我不确定这些内容在那种情况下是否有效。

There's a related answer that shows how to access the form elements via the template object which is an optional second parameter to your event handler. 有一个相关的答案 ,显示了如何通过template对象访问表单元素, template对象是事件处理程序的可选第二个参数。 That might also help you. 这也可能对您有帮助。

When I'm debugging things like this I normally set a breakpoint (using the browser's development tools) at the first line of the event handler then inspect the event to make sure I understand what's inside. 当我调试类似的东西时,我通常在事件处理程序的第一行(使用浏览器的开发工具)设置一个断点,然后检查事件以确保我了解其中的内容。

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

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