繁体   English   中英

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

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

我想单击按钮2以在按钮1上触发单击事件。我可以使用JQuery完成此任务。

HTML:

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

JS:

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

我是Ember.js的新手,我想在Ember.View环境中完成相同的功能。 预先感谢您的帮助。

只需在输入上使用change事件。 然后,您可以发布表单,或使用ajax异步回发。 这是一个简单的例子:

视图

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
    });
  }
});

模板

{{view 'uploadButton'}}

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

暂无
暂无

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

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