我希望使用用户提交的网址将 Youtube 视频嵌入到 Meteor 项目中。我只想收集用户 Youtube URL 末尾的 11 个字符 ID。有人似乎在这里找到了浏览器当前网址的一些信息。我只需要针对表单中提供的网址提供类似的解决方案:
How to get anything following the domain in a url, using Javascript
任何人都可以帮助这个插入语句做到这一点吗?任何帮助将不胜感激。
Template.vidForm.events({
'submit #vid-form' : function(e, t) {
var title = t.find('#vid-title').value
, url = t.find('#vid-url').value;
if (isNotEmpty(title)
&& isNotEmpty(url))
{
vids.insert({user: Meteor.userId(), username: Meteor.user().username, date: new Date(), title: title, url: url}, function(err){
if (err)
Alerts.add('Submission error: ' + err.reason, 'warning');
else {
Alerts.add('Your video has been accepted.', 'success');
}
}
);
}
return false;
}
});
最佳答案
这就是我的决定。我利用了这里的答案之一:How to replace multiple strings with the .replace() Method?
Template.vidForm.events({
'submit #vid-form' : function(e, t) {
var title = t.find('#vid-title').value,
youtube = t.find('#vid-url').value,
url = youtube.replace(/http:|https:|www.|youtube.com|watch\?v=|youtu.be|\//g,'');
if (isNotEmpty(title)
&& isNotEmpty(url))
{
vids.insert({user: Meteor.userId(), username: Meteor.user().username, date: new Date(), title: title, url: url}, function(err){
if (err)
Alerts.add('Submission error: ' + err.reason, 'warning');
else {
Alerts.add('Your video has been accepted.', 'success');
}
}
);
}
return false;
}
});
关于javascript - 使用 Javascript 从用户提交的 Youtube URL 收集 ID(对于 Meteor 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446801/