I've got a basic front-end-only web app where a button gets clicked, a GET Request is sent to the Foursquare API, and then AJAX stores some of the request's response data into a div as follows:
我有一个基本的前端Web应用程序,其中点击一个按钮,GET请求被发送到Foursquare API,然后AJAX将一些请求的响应数据存储到div中,如下所示:
checkinsCount[i] = data.response.venues[i].stats.checkinsCount;
$("#fs_results").data("checkinsCount", checkinsCount);
d = $("#fs_results").data("checkinsCount");
console.log(d);
I then have a checkbox, where I want to display the data in a div when the checkbox is clicked, but ONLY after verifying that the data exists already.
然后我有一个复选框,我想在单击复选框时在div中显示数据,但仅在验证数据已存在之后。
So I'm trying to use jQuery's .hasData() to check this:
所以我试图使用jQuery的.hasData()来检查:
$('#checkbox1').click(function () {
if($.hasData($('#fs_results'))){
$("#fs_results").toggle(this.checked);
} else{
alert("false");
$('#checkbox1').attr('checked', false);
}
});
But right now, I'm just getting the false alert repeatedly, even when I have verified the data is present. I'm lost as this is my first time using jQuery/AJAX...any help or suggestions?
但是现在,我只是反复得到虚假警报,即使我已经验证数据存在。我迷路了,因为这是我第一次使用jQuery / AJAX ...任何帮助或建议?
1 个解决方案
#1
2
hasData operates on a single element i believe (see docs), you are passing in a query result.
hasData对我认为的单个元素进行操作(参见docs),你传递的是查询结果。
Thus replacing
if($.hasData($('#fs_results'))){
with
if($.hasData($('#fs_results')[0])){
should solve the problem. Separately, you should be using prop()
and not attr()
on the checkbox.
应该解决问题。另外,你应该在复选框上使用prop()而不是attr()。
#1
2
hasData operates on a single element i believe (see docs), you are passing in a query result.
hasData对我认为的单个元素进行操作(参见docs),你传递的是查询结果。
Thus replacing
if($.hasData($('#fs_results'))){
with
if($.hasData($('#fs_results')[0])){
should solve the problem. Separately, you should be using prop()
and not attr()
on the checkbox.
应该解决问题。另外,你应该在复选框上使用prop()而不是attr()。