简体   繁体   English

按钮单击在 jquery 中不起作用

[英]button click is not working inside jquery

I am new to html and java script.我是 html 和 java 脚本的新手。 I try to create a button and get the JSON back from a URL and add the result to a drop down list.我尝试创建一个按钮并从 URL 获取 JSON 并将结果添加到下拉列表中。 After the Nodejs server is up and running, if you enter http://localhost:8080/restapi/test/projects , it will return {example} as the result.在 Nodejs 服务器启动并运行后,如果您输入http://localhost:8080/restapi/test/projects ,它将返回 {example} 作为结果。

So there are couple questions here: 1) for some reason, the button click is not working inside jquery 2) $.getJSON can only used inside jquery, is there any other way to obtain JSON from a URL respond?所以这里有几个问题:1)出于某种原因,按钮单击在 jquery 中不起作用 2)$.getJSON 只能在 jquery 中使用,还有其他方法可以从 URL 响应中获取 JSON 吗?

 $(document).ready(function () { $(document).on('click', '#button1', function() { var selector = document.getElementById('selector'); var api = 'http://localhost:8080/restapi/test/projects'; $.getJSON(api, function (data) { $.each(data, function (index, d) { var option = document.createElement('option'); option = d; selector.add(option); }); }) }) })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div id='first'> <h1>project options</h1> <button id='button1'>Get Projects</button> </div> <div id='second'> <h2>all projects</h2> Projects: <select id='selector'></select> </div>

Thanks for any tips.感谢您提供任何提示。

{example} is not valid JSON. {example}不是有效的 JSON。 Try {"1":"example"}试试{"1":"example"}

Also, option = d ;另外, option = d ; will overwrite your option.将覆盖您的选项。 Try option.value = d;试试option.value = d;

Here is a cleaned up version for you :这是一个清理过的版本给你:

$(document).ready(function () {
    $('#button1').click(function() {
        var selector = document.getElementById('selector');
        var api = 'http://localhost:8080/restapi/test/projects';
        $.getJSON(api, {"1":"example","2":"other example"},function (data) {
            $.each(data, function (index, d) {
                selector.options[selector.options.length] = new Option(d,d);
            });
        });
    });
});

Fiddle: https://jsfiddle.net/trex005/65hc1srh/小提琴: https : //jsfiddle.net/trex005/65hc1srh/

Try the following:请尝试以下操作:

$(document).ready(function () {
    $('body').on('click', '#button1', function() {//this sintax is used most for dynamically added elements
        var selector = $('#selector');
        var api = 'http://localhost:8080/restapi/test/projects';
        $.getJSON(api, function (data) {
            $.each(data, function (index, d) {
                var option ='<option>'+d+'</option>';
                selector.append(option);
            });
        })
    })
})

the click button is actually working... maybe there's a problem with the response.单击按钮实际上正在工作...也许响应有问题。 you can check the network log by opening the inspector.您可以通过打开检查器来检查网络日志。 to open the inspector on chrome use right click and then 'inspect' or 'inspect element'.要在 chrome 上打开检查器,请使用右键单击然后“检查”或“检查元素”。

this is what happened when i click the button.这是我单击按钮时发生的情况。

for the second question you can make an AJAX request using the XMLHttpRequest object.对于第二个问题,您可以使用 XMLHttpRequest 对象发出 AJAX 请求。

It will not worked because you did not do the exact thing clicked.All you have done is that $(document).click() which don't know what elements or document to click.它不会起作用,因为您没有执行单击的确切操作。您所做的只是$(document).click()不知道要单击哪些元素或文档。

You should have used你应该用过

 $('#button1').on('click',function() {

        });

for button click which tells that it will response only when the button is clicked.For your better understanding I am giving the code snippet用于按钮单击,它告诉它只有在单击按钮时才会响应。为了您更好地理解,我给出了代码片段

 $(document).ready(function () { $('#button1').on('click',function() { var selector = document.getElementById('selector'); var api = 'http://localhost:8080/restapi/test/projects'; $.getJSON(api, function (data) { $.each(data, function (index, d) { var option = document.createElement('option'); option = d; selector.add(option); }); }); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div id='first'> <h1>project options</h1> <button id='button1'>Get Projects</button> </div> <div id='second'> <h2>all projects</h2> Projects: <select id='selector'></select> </div>

It will work.If not then please check your api url.它会工作。如果没有,请检查您的 api 网址。

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

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