繁体   English   中英

jQuery和它的.getJSON不起作用,以及如何同步asynchronus .getJSON?

[英]jQuery and its .getJSON not working and also how to synchronise the asynchronus .getJSON?

首先,是的,我知道有几个答案,但没有一个能够解决我的问题。 首先,我将展示我的代码HTML:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shop</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="https://use.fontawesome.com/a565674a9d.js"></script>


    <!--MINE-------------------------------->
    <script src="JS/jquery.js"></script>
    <script src="JS/catalog.js"></script>

</head>
<body>
<nav class="navbar navbar-light">
    <div>
    <a class="navbar-brand" style="text-align: center;position: fixed; left: 40%;" >
        <img src="979709898.png" width="70" height="70" class="d-inline-block align-top" alt="">
        Costumer Bay
    </a>
        <a id="cart" class=" navbar-brand btn btn-danger" style="position: fixed; left: 70%" >
            <i class="fa fa-shopping-cart fa-3x" aria-hidden="true"></i>
        </a>
    </div>
</nav>
<div id="lin1" class="container-fluid" style="padding: 20px;">

</div>
<div id="lin2" class="container-fluid" style="padding: 20px;">

</div>
</body>
</html>

jquery.js是未压缩的原始jquery源代码

Data.Json位于JS文件夹中

data.json:

[
    {
        "id":0,
        "name":"Laptop",
        "price":50000 ,
        "quantity":0
    },
    {
        "id":1,
        "name":"Phone",
        "price":30000 ,
        "quantity":0
    },
    {
        "id":2,
        "name":"Desktop",
        "price":70000 ,
        "quantity":0
    },
    {
        "id":3,
        "name":"Headphone",
        "price":9000 ,
        "quantity":0
    },
    {
        "id":4,
        "name":"Tablet",
        "price":35000 ,
        "quantity":0
    },
    {
        "id":5,
        "name":"AntiVirus",
        "price":5000 ,
        "quantity":0
    }
]

现在终于javascript无效了

catalog.js

let productinfo=[];
let firsttime = true;

$(function () {
console.log("here");
    refreshcatalog();
    console.log("now where");
    let cart = $('#cart');
    cart.click(function () {          //NOT!WORKING
        loadcart();
    })

});

function refreshcatalog() {
    console.log("no where");
    if (firsttime){
        console.log('evenhere');
        $.getJSON("JS/data.json",function (data) {
            console.log(data);                         //NOT!WORKING
        });
    }
    if(firsttime){
    firsttime=!firsttime;}
}
function loadcart() {
    console.log("will load cart");
}

即使是点击事件也不会运行。 请指导我,不,我不知道AJAX,所以请不要使用ajax方法来解决。

请帮忙

<---- EDIT1 ------>

for all those asking for directory structure:
shopping-----
             |_  catalog.html

             |_  JS(folder) -----
                                 |_  catalog.js
                                 |_  jquery.js
                                 |_  data.json

<--------------------- EDIT 2 -__----------->

更改格式后,Click事件开始运行:

$(document).on('click','#button2', function()
{
    alert("Dynamic button action");
});

我旧的“.click”无法正常工作的原因,将不胜感激。我正在使用Firefox开发者版。

下面的代码需要更改,因为catalog.js和data.json位于同一个文件夹中。

$.getJSON("JS/data.json",function (data) 
    console.log(data);
});

将代码更改为

$.getJSON("data.json",function (data) {
    console.log(data);
});

请检查并告诉我您是否能够控制数据,然后将解决其他问题。

让我们重新排列脚本(订购)

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

我已经删除了jquery slim,因为bootstrap使用它而不是我已经将你的jquery复制到顶部,以便bootstrap可以使用它。

这是编辑过的部分。

请不要从$ .getJSON中删除JS / data.json。

删除你的jquery slim和JS / jquery。 尝试使用以下CDN链接或我在评论中提供

它在我当地为我工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM