简体   繁体   English

在输入文件类型中选择文件时,如何触发按钮单击事件?

[英]How to fire button click event when file selected in input file type?

I have a file <input /> and a <button> , with a click handler assigned to the button. 我有一个文件<input />和一个<button> ,并为按钮分配了一个单击处理程序。

What I would like to do is to execute the click handler on the submit button when the selected file changes on the file input. 我想做的是,当所选文件在文件输入上更改时,在提交按钮上执行单击处理程序。

My code currently looks like this: 我的代码当前如下所示:

 angular.module('myapp', []) .controller('MyController', function($scope) { $scope.clickMe= function(){ alert("File Submitted!"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="myapp"> <div ng-controller="MyController"> <input type = "file"> <div><button ng-click="clickMe()">Submit</button></div> </div> </body> 

If I understand your question correctly, then you should find that the logic fired when the submit button is clicked, can instead be automatically invoked when a file is picked on your <input type="file" /> element by updating your template as follows: 如果我正确理解了您的问题,那么您应该发现单击“提交”按钮时触发的逻辑可以改为通过按如下方式更新模板在<input type="file" />元素上选择文件时自动调用的逻辑:

<input type="file" onchange="angular.element(this).scope().clickMe(this)"> 

This will cause the clickMe() function on the $scope object of the enclosing controller MyController , to be called. 这将导致封闭控制器MyController$scope对象上的clickMe()函数被调用。 Here's a complete example (with submit button removed seeing it's redundant): 这是一个完整的示例(删除提交按钮会看到多余的示例):

 angular.module('myapp', []) .controller('MyController', function($scope) { $scope.clickMe = function() { alert("File Submitted!"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.7/angular.min.js"></script> <body ng-app="myapp"> <div ng-controller="MyController"> <input type="file" onchange="angular.element(this).scope().clickMe(this)"> </div> </body> 

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

相关问题 在文件输入Ionic 3 Android中触发点击事件 - Fire click event in file input ionic 3 android 输入文件事件 onchange 按钮单击输入文件时不起作用 - input file event onchange no work when button click input file 将更改事件绑定到输入类型文件,并在每次选择文件时触发它IE 8 - Bind Change Event to input type file and have it fire every time a file is selected IE 8 如何在 onload 事件上单击输入(类型=文件) - How to click an input (type=file) on onload event 以编程方式在输入类型文件上触发 onchange 事件 - Fire onchange event on input type file programmatically Firefox 上的输入类型编号:单击旋转按钮不会触发焦点事件 - input type number on Firefox: click on spin button not fire focus event 问题:如果两次选择了同一个文件,则不会为Input触发OnChange事件 - Issue: OnChange event does not fire for Input if it is same file is selected twice 触发javascript事件的onchange事件<input type=“file”/> - Fire javascript event onchange event of <input type=“file”/> 如果用户在输入框中键入,如何触发单击按钮 - How to fire click on a button if the user type in an input box 调用用户定义的函数时,不会触发输入type = file onchange事件 - Input type=file onchange event won't fire when calling a user-defined function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM