我一直在研究这个问题,似乎无处可去.基本上我在类的一堆输入上有自动完成,但是我需要获取特定的输入id来构建对象以发布ajax(我必须为这个项目使用POST而不是GET).
$(".input_autocomplete").autocomplete({
source: function( request, response ) {
// here is where I get the hash from local storage,
// reference the id and build the object
// tried this.id, $(this).prop('id'), no luck
$.ajax({
url: '/somepath/filename.htm',
contentType: 'application/json',
dataType: 'json',
type: 'POST',
data: JSON.stringify(obj),
success: function(json) {
return {
label: item.label,
value: item.label
}
},
error: function() {
// error handling
}
}); // ajax
} // source
});
Run Code Online (Sandbox Code Playgroud) 有没有人有一个很好的例子,在keystonejs中为关系字段添加名称?现在它只保存一个id,所以如果我需要在玉器模板中显示那些字段名称,我还需要查询该关系模型.理想情况下是这样的:
var keystone = require('keystone'),
Types = keystone.Field.Types;
/**
* Titles Model
* =============
*/
var Title = new keystone.List('Title');
Title.add({
name: { type: String, required: true, initial: true },
room: { type: Types.Relationship, initial: true, required: true, ref: 'Screening', addNew: false },
businessUnit: { type: Types.Relationship, initial: true, required: true, ref: 'BusinessUnit', addNew: false }
});
Title.defaultSort = '-createdAt';
Title.defaultColumns = 'name, room';
Title.register();
Run Code Online (Sandbox Code Playgroud)
会像这样保存:
title = {
name: 'name',
room: 3141234123442,
businessUnit: {
name: 'business name',
_id: 123412341234 …Run Code Online (Sandbox Code Playgroud) JSlint 抱怨不必要的转义字符,但我似乎不明白为什么。这是我的正则表达式,有什么想法吗?
不必要的转义字符:[ no-useless-escape
const _emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const _phoneRegex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
Run Code Online (Sandbox Code Playgroud) 我向数据库发布了一个新行,但希望使用medoo在响应中返回新的自动增加的id.这似乎是生成一个新的空行并返回该id.
<?php
// Independent configuration
require 'medoo.php';
$database = new medoo(array(
// required
'database_type' => 'mysql',
'database_name' => 'db',
'server' => 'server',
'username' => 'user',
'password' => 'pw'
));
$database->insert("properties", array(
"name" => $_POST['name'],
"address" => $_POST['address'],
"address2" => $_POST['address2'],
"city" => $_POST['city'],
"state" => $_POST['state'],
"zip" => $_POST['zip'],
"lat" => $_POST['lat'],
"lng" => $_POST['lng'],
"website" => $_POST['website']
));
$last_id = $database->insert("properties", array(
"propertyId" => "propertyId"
));
$propertyId = array(propertyId => $last_id);
echo json_encode($propertyId);
?>
Run Code Online (Sandbox Code Playgroud) 我试图在第一个函数的fetch成功调用另一个函数,但我一直得到一个未定义的错误.console.log('success')正在运行.
在视图中:
secondFunction: function() {
console.log('second function called');
},
someFunction: function() {
someData.fetch({
success: function(results) {
console.log('success');
this.secondFunction();
}
});
},
Run Code Online (Sandbox Code Playgroud)
当我这样做时它很好,但我需要secondFunction等到数据的第一个成功.
secondFunction: function() {
console.log('second function called');
},
someFunction: function() {
someData.fetch({
success: function(results) {
console.log('success');
}
});
this.secondFunction();
},
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何编写一个正则表达式,从这样的字符串提供url:
'<script type="text/javascript" src="http://chrismills.la/test.js"></script>';
Run Code Online (Sandbox Code Playgroud)
这有效,但我需要找到一种更简洁的方法来做到这一点:
var url = '';
_content.replace(/src ?= ?"([^"]+)"/gi, function(res) {
url = res;
url = url.replace('src=','');
url = url.replace(/['"]+/g, '')
url = url.replace(/["']+/g, '')
});
script.src = url;
Run Code Online (Sandbox Code Playgroud)
在行动:http: //jsfiddle.net/zb3jh180/
解决方案,基于以下答案:
var regex = /<script.*?src="(.*?)"/gmi;
var url = regex.exec(_content);
script.src = url[1];
Run Code Online (Sandbox Code Playgroud) 我正在为一个库创建一个函数,它接受一个元素对象并将其从DOM中删除.我有这个工作正常,但想知道是否有办法做一个for循环?我发现NodeLists并且HTMLCollections不能使用相同的for循环所以我构建一个数组然后删除具有相同循环的元素.
_remove = function(elem) {
if(!elem) { return false; }
// getElementById
if(!elem.length) {
if(elem.parentNode) {
elem.parentNode.removeChild(elem);
}
// querySelectorAll, jquery, getElementsByClassName, getElementsByTagName
} else {
var elems = [];
for(var j = 0; j<elem.length; j++) {
if(elem[j]) {
elems.push(elem[j]);
}
}
for(var i=0; i<elems.length; i++) {
if(elems[i].parentNode) {
elems[i].parentNode.removeChild(elems[i]);
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
叫做:
_remove(document.getElementById('someId'));
_remove(document.getElementsByClassName('someClass'));
_remove(document.getElementsByTagName('tag'));
_remove($('#someId));
_remove($('.someClass));
_remove(document.querySelectorAll('someID or Class or Tag));
Run Code Online (Sandbox Code Playgroud) javascript ×3
regex ×2
backbone.js ×1
ecmascript-6 ×1
jquery ×1
jquery-ui ×1
jslint ×1
keystonejs ×1
mongoose ×1
node.js ×1
php ×1