[英]ENTER keydown doesn't work in IE! Angular
I have an input field where users insert a video url, and when press 'enter', video player display that video. 我有一个输入字段,用户可以在其中输入视频网址,然后按“输入”,视频播放器将显示该视频。 It works fine in Chrome, Firefox, Edge but doesn't work in IE 11. It starts working only if I open the IE developer tools and then go back to page to press enter.
它在Chrome,Firefox,Edge中可以正常工作,但在IE 11中则不能工作。只有当我打开IE开发人员工具然后返回页面以按Enter时,它才能开始工作。
Here is my code 这是我的代码
HTML HTML
<form>
<label for="videoUrlField">Paste your video URL here: </label>
<input autocomplete="off" type="url" id="videoUrlField" enter-listener>
</form>
Controller.js Controller.js
app.directive('enterListener', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
element.bind("keydown", function (event) {
var validURL = function (str) {
var urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
return urlregex.test(str);
};
if (event.which === 13) {
var videoUrl = ''+element.val()
//if url is valid then let's load it
if(validURL(videoUrl)) {
playApropriateType(videoUrl);
}
else{alert("Wrong URL")}
}
})
}
}
}); });
Could anyone tell me what is wrong with IE or my code? 谁能告诉我IE或我的代码出了什么问题?
Angular supports ng-submit
for forms out of the box. Angular支持
ng-submit
开箱即用的表单。 Have you tried ngSubmit on your form element? 您是否在表单元素上尝试了ngSubmit?
<form ng-submit="myEnterFunction()">
<label for="videoUrlField">Paste your video URL here: </label>
<input autocomplete="off" type="url" id="videoUrlField" enter-listener>
</form>
Where myEnterFunction()
can be a controller function to handle / validate / ... upon enter. 其中
myEnterFunction()
可以是控制器函数,用于在输入时处理/验证/...。 In my opinion a much cleaner and more AngularJS way to go. 我认为这是一种更清洁,更AngularJS的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.