【问题标题】:Whats the difference between $(e.target).find and template.find('input').value$(e.target).find 和 template.find('input').value 有什么区别
【发布时间】:2016-08-30 20:28:45
【问题描述】:

它可能是一些基本的但想要解释用例的东西。就像有时点击“输入”输入数据,而有时鼠标点击工作。我担心我会忽略的“陷阱”。例如,它可能在 Firefox 中有效,但在 Chrome 中无效。

我看到了以下两种方式,都是将数据输入到表单元素中的方式。

第一种方式

JavaScript

var $body = $(e.target).find('[name=body]');  //defines the content
var comment = { body: $body.val() };    

HTML

<form class="form-send-message" id="addcomment" data-keyboard-attach>
  <textarea id="body" name="body"></textarea>
</form>

第二种方式

JavaScript

var message = template.find('input').value;

HTML

<form class="message" data-keyboard-attach>
    <input type="text" name="body" id="body">
    <button class="icon" type="submit"></button> 
</form>

【问题讨论】:

  • 两个不同的 JS 库?

标签: javascript jquery html meteor


【解决方案1】:

在这里您可以看到两种方法来查找输入/文本区域的值并附上解释:

'submit .new-post': function(event){
    //returns name="postBody" content from the form you're submitting
    var postBody = event.target.postBody.value;  

    //returns the value of an html element that exists in DOM, even if its inside a different template or form.
    var postBody = $('.someClass').val()  
}

【讨论】:

    【解决方案2】:

    您的第一个代码是jQuery,而您的第二个代码是Meteor。他们都可以在适当的情况下完成同样的事情。另外,根据this answer,meteor 的template.find 是jQuery 的$ 的别名,意思是完全一样的。

    但是,在这种情况下,代码不会做同样的事情。

    您的第一个代码在e.target 中查找名称为“body”的元素的值。我假设eEvent,但无法判断您提供的当前代码量。

    第二个代码只是获取它找到的第一个 INPUT 元素的值。

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2017-02-16
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 2016-11-22
      相关资源
      最近更新 更多