简体   繁体   English

Ember.js - 单击一个按钮可触发另一个按钮上的点击事件

[英]Ember.js - Clicking on one button to trigger click event on another

I want to click on button 2 to trigger a click event on button 1. I can accomplish this tasks using JQuery. 我想单击按钮2以在按钮1上触发单击事件。我可以使用JQuery完成此任务。

HTML: HTML:

<div id="container">
<input id="upload_input" type="file" name="files[]" style="display:none" />
<button id="button-2">Upload File</button>
</div>

JS: JS:

$('#upload_input').trigger('click');

I'm new to Ember.js and I would like to accomplish the same functionality within the Ember.View context. 我是Ember.js的新手,我想在Ember.View环境中完成相同的功能。 Thank you in advance for your help. 预先感谢您的帮助。

Just use the change event on the input. 只需在输入上使用change事件。 You can then post the form, or use ajax to post back asynchronously. 然后,您可以发布表单,或使用ajax异步回发。 Here's a simple example: 这是一个简单的例子:

View 视图

App.UploadButtonView = Ember.View.extend({
  tagName: 'input',
  attributeBindings: ['type'],
  type: 'file',

  change: function(e){
    var self = this;
    var formData = new FormData();
    formData.append('file', this.$().get(0).files[0]);
    $.ajax({
      url: '/file_upload/', 
      type: 'POST',
      //Ajax events
      success: function(data){ },
      error: function(){ },
      // Form data
      data: formData,
      //Options to tell jQuery not to process data or worry about content-type.
      cache: false,
      contentType: false,
      processData: false
    });
  }
});

Template 模板

{{view 'uploadButton'}}

Example: http://emberjs.jsbin.com/jameg/1/edit 示例: http//emberjs.jsbin.com/jameg/1/edit

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

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