AJAX - 点击按钮不起作用

时间:2021-11-22 09:27:34

as title suggests, I am having issues running my script:

正如标题所示,我在运行脚本时遇到问题:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#subbut").click(function(e){
          e.preventDefault();
        $.ajax(
                    {
                        url : 'http://jsonplaceholder.typicode.com/users?email=Sincere@april.biz',
                        method : 'GET'
                    })
            .then(
                    function(data) {

                        document.getElementById("street").value = data[0].address.street;

                    });
    });
});
</script>

And my form is here:

我的表格在这里:

<form id="myForm"
            action="<%=response.encodeURL(request.getContextPath()
                + "/upisKorisnika.html")%>"
            method="POST">
            <table id='userInput' class="display" border="0"
                style="font-family: Arial; font-size: 18px;">

                <tr>
                    <td><c:out value="E-mail: " /></td>
                    <td><input type="text" name="email" id="email"></td>

                </tr>

                <tr>
                    <td colspan="2" style="text-align: center">
                        <input type="submit" name="subbut" class="btn btn-default" id="subbut" value="Submit">
                    </td>
                </tr>
            </table>
            <input type="hidden" id="street" name="street" />
            </form>

My first question is why doesn't anything happen when I click submit, what am I doing wrong? And I would also like to know how could I get value of text field "Email", so I could use something like this:

我的第一个问题是,当我点击提交时,为什么没有发生任何事情,我做错了什么?而且我也想知道如何获得文本字段“Email”的价值,所以我可以使用这样的东西:

url : 'http://jsonplaceholder.typicode.com/users?email='+mail,

1 个解决方案

#1


First, is confusing the use of a form that not send data to the server and a submit button without submit form behavior.

首先,混淆使用不向服务器发送数据的表单和没有提交表单行为的提交按钮。

Second you need use a debug tool like chrome developer tool. It will give you the trace of your calls.

其次,你需要使用像Chrome开发工具这样的调试工具。它会给你跟踪你的电话。

To debug, add to the promise.then the error callback with a simple alert or a console.log to view what is happen with your call.

要进行调试,请添加到promise.then使用简单警报或console.log进行错误回调,以查看调用发生的情况。

And responding your question,your code seen be right.

并回答您的问题,您的代码是正确的。

#1


First, is confusing the use of a form that not send data to the server and a submit button without submit form behavior.

首先,混淆使用不向服务器发送数据的表单和没有提交表单行为的提交按钮。

Second you need use a debug tool like chrome developer tool. It will give you the trace of your calls.

其次,你需要使用像Chrome开发工具这样的调试工具。它会给你跟踪你的电话。

To debug, add to the promise.then the error callback with a simple alert or a console.log to view what is happen with your call.

要进行调试,请添加到promise.then使用简单警报或console.log进行错误回调,以查看调用发生的情况。

And responding your question,your code seen be right.

并回答您的问题,您的代码是正确的。