我想在输入文本框中停止输入(而不是在输入时)字符后触发事件.
我尝试过:
$('input#username').keypress(function() {
var _this = $(this); // copy of this object for further usage
setTimeout(function() {
$.post('/ajax/fetch', {
type: 'username',
value: _this.val()
}, function(data) {
if(!data.success) {
// continue working
} else {
// throw an error
}
}, 'json');
}, 3000);
});
Run Code Online (Sandbox Code Playgroud)
但是这个例子会为每个类型字符生成一个超时,如果输入20个字符,我会得到大约20个AJAX请求.
在这个小提琴上,我用简单的警报而不是AJAX来演示同样的问题.
有没有解决方案,或者我只是使用一种不好的方法?
我正在使用它将youtube网址转换为嵌入网址.
text(t).html().replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com)\/(?:watch\?v=)?(.+)/g, '<iframe width="320" height="280" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>')
Run Code Online (Sandbox Code Playgroud)
我怎么能让它自己忽略?
t = $('<div></div>').text(t).html().replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com)\/(?:watch\?v=)?(.+)/g, '<iframe width="400" height="380" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>')
Run Code Online (Sandbox Code Playgroud)
和嵌入式链接
<iframe width="560" height="315" src="//www.youtube.com/embed/1adfD9" frameborder="0" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)
或者,换句话说,我怎样才能使它只在这样的链接上工作而忽略其他一切?
http://www.youtube.com/watch?v=1adfD9
www.youtube.com/watch?v=1adfD9
youtube.com/watch?v=1adfD9
Run Code Online (Sandbox Code Playgroud) 我需要检查,如果Youtubeurl不存在我应该抛出错误,我按照这个答案并创建了jsfiddle来检查它.
它适用于有效网址但不适用于无效网址.我只看到404错误network console

有没有办法检查客户端是否存在url使用JavaScript和jQuery.
这是我的代码:
var videoID = 'kn8yzJITdvI';//not working
//var videoID = 'p4kIwWHP8Vc';//working
$.ajax({
url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
dataType: "jsonp",
success: function(data) {
console.log(data)
$("#result").text(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
alert('ERRORS: ' + textStatus);
}
});
Run Code Online (Sandbox Code Playgroud)
jquery ×3
javascript ×2
ajax ×1
debouncing ×1
keypress ×1
timeout ×1
typeahead ×1
youtube ×1
youtube-api ×1