为什么使用按钮或Div运行查询会返回不一致的结果?
Why does a query run using either a button or a Div return inconsistent results?
使用JS/JQuery。
我想让用户做的是单击显示在页面上的名称,并复制相同的操作,如果用户单击"查找"按钮会发生什么。基本上是将相关结果返回到页面。
示例页面在这里,http://www.kudosoo.com/friendslist.html因此,例如,用户单击"dave",结果与他们在文本框中输入"dave"并单击"find"
相同当点击为什么<div id="container"></div>
不返回相同的结果点击"查找"按钮,他们都使用相同的查询"friendFeed"
也许是字符串存储方式的问题?
<!doctype html>
<!-- Runs Parse and FB code that uses Facebook authentication to login user to the site and redirects them to the main content area. This page is fired from the Facebook button being clicked on the site landing page-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="fresh Gray Bootstrap 3.0 Responsive Theme " />
<meta name="keywords" content="Template, Theme, web, html5, css3, Bootstrap,Bootstrap 3.0 Responsive Login" />
<meta name="author" content="Adsays" />
<title>Parse JavaScript Todo App</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.17.min.js"></script>
</head>
<body>
<!-- Initialize the Parse object-->
<script type="text/javascript">
Parse.initialize("79tphN5KrDXdjJnAmehgBHgOjgE2dLGTvEPR9pEJ", "9lblofQNZlypAtveU4i4IzEpaOqtBgMcmuU1AE6Y");
// capture name of user thats been clicked
// return their activty feed of badges etc
var currentUser = Parse.User.current();
var FriendRequest = Parse.Object.extend("FriendRequest");
var query = new Parse.Query(FriendRequest);
query.include('toUser');
// query.include('fromUser');
//query.include('pic');
query.equalTo("fromUser", currentUser);
query.equalTo("status", "Request sent");
query.find({
success: function (results) {
// If the query is successful, store each image URL in an array of image URL's
imageURLs = [];
Username = [];
for (var i = 0; i < results.length; i++) {
var object = results[i].get("toUser");
imageURLs.push(object.get("pic"));
Username.push(object.get("username"));
}
// If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
console.log(imageURLs);
console.log(Username);
for (var j = 0; j < imageURLs.length; j++) {
$('#container').append("<img class='images' src='" + imageURLs[j] + "'/>");
$('#container').append("<div class='username'>'" + Username[j] + "'</div>");
}
},
error: function (error) {
// If the query is unsuccessful, report any errors
alert("Error: " + error.code + " " + error.message);
}
});
var currentUser = Parse.User.current();
var myBadges = Parse.Object.extend("myBadges");
// Captures the input from the user and checks if the name already exists within the Db.
function friendFeed(name) {
var friendName = name || $('#friendsearch').val();
console.log(friendName);
var query = new Parse.Query(myBadges).matchesQuery("uploadedBy", new Parse.Query("_User").equalTo("username", friendName));
query.find({
success: function (rules) {
imageURLs = [];
for (var i = 0; i < rules.length; i++) {
var object = rules[i];
imageURLs.push(object.get("BadgeName"));
Username.push(object.get("uploadedBy"));
}
for (var j = 0; j < imageURLs.length; j++) {
$('#FriendsStuff').append("<img class='images' src='" + imageURLs[j] + "'/>");
$('#FriendsStuffName').append("<div class='username'>'" + Username[j] + "'</div>");
console.log("here");
}
},
error: function (error) {
//If the query is unsuccessful, report any errors
alert("Error: " + error.code + " " + error.message);
}
});
}
$(document).ready(function () {
$(document).on('click', '.username, #find_button', function (event) {
//debugger;
event.preventDefault();
friendName = $(this).is("button") ? null : $(this).text();
friendFeed(friendName);
});
});
</script>
<div class="error" style="display:none"></div>
<input type="text" id="friendsearch" placeholder="Find Friend" class="input-field" />
<button id="find_button" type="button" class="btn btn-login">Find</button>
<div id="container"></div>
<div id="FriendsStuff"></div>
<div id="FriendsStuffName"></div>
</body>
</html>
你需要的是一个事件的点击戴夫,从类。username
请添加:
$(document).on('click', '.username', function (event) {
//debugger;
event.preventDefault();
friendName = $(this).innerHTML; // get content of user
friendFeed(friendName);
});
就我个人而言,我更喜欢存储变量,将用于不同的函数属性的元素,你定义和检索$(this).attr("YOUR_ATTRIBUTE_NAME")而不是获取div
的innerHTML并将字符串存储在:<div class=".username" YOUR_ATTRIBUTE_NAME="Dave">Dave</div>
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- Android和JavaScript解析API之间不一致
- Firefox使用JQuery返回不正确的JSON
- Javascript的行为与PHP包含文件不一致
- 对这种与document.domain和CORS相关的不一致行为的解释是什么
- 点击链接行为不一致
- 为什么 alert() 在输入不一致的位数后返回未定义
- 返回不一致结果的日期(取决于是否存在前导零)
- 新日期(UNIX)返回不一致的时区
- Facebook Javascript SDK getLoginStatus 返回未知(完全不一致)
- 我设置的高度和宽度值与返回的值不一致的原因是什么
- 角度带日期选择器:返回不一致的时间戳,UTC+0与UTC+12小时
- 函数的返回点不一致
- 如何用JSDoc记录不一致的返回
- Javascript在Firefox for Android中随机返回不一致的高度
- 为什么使用按钮或Div运行查询会返回不一致的结果?
- 从WCF服务返回的Json日期不一致
- 谷歌闭包编译器w/ ternaries: ERROR -不一致的返回类型
- 函数返回点不一致的JavaScript