javascript - 如何在不刷新/提交页面的情况下发送 POST 方法

标签 javascript java html

我正在尝试更新数据库中的特定字段,即从电话模型中“分配”的字段。 我尝试在 javascript 中创建一个函数 (setBooleanForCurrentPhone()),它会在 select 的 onchange 中将 id="assignPhone"的值从“true”更改为“false”。

问题是“分配”在数据库中没有改变,因为我还没有提交任何东西。

此外,我尝试制作一个按钮,只是为了测试它,它会调用 setBooleanForCurrentPhone() 函数 onclick,但问题是 这是提交数据,页面跳转到另一个页面,我没有使用最后一个调用 setBooleanForPhone() 的提交按钮。

基本上,我想找到一种在不刷新/更改页面的情况下从数据库更新该值的方法。

            <tr>
                <td>Employee id:</td>
                <td><input type = "text" th:field="*{id}" readonly="readonly" ></td>
            </tr>

            <tr>
                <td>Employee last name:</td>
                <td><input type = "text" th:field="*{lastName}" ></td>
            </tr>

            <tr>
                <td>Employee first name:</td>
                <td><input type = "text" th:field="*{firstName}" ></td>
            </tr>

            <tr>
                <td>Phone:</td>
                <td style="display: none"><input type="text" id="destination" th:field="*{phone}"></td>
                <td style="display: none"><input type="text" id="assignPhone" th:field="*{phone.assigned}"></td>
                <td>
                    <select id="source" onchange="copyTextValue(), setBooleanForCurrentPhone()" th:field="${possiblePhones}" >
                        <option th:value="${employee.phone?.id}" th:text="${employee.phone != null ? employee.phone.brand +' '+ employee.phone.model : 'no phone'}">select phone</option>
                        <option th:each="possiblePhone : ${possiblePhones}" th:value="${possiblePhone.id}" th:text="${possiblePhone.brand +' '+ possiblePhone.model} "></option>
                    </select>
                </td>
            </tr>

            <!--submit button-->

            <tr>
                <td colspan="2">
                    <button onclick="setBooleanForPhone()" type="submit">Save</button>
                </td>
            </tr>
        </table>

我在互联网上找到了一些东西,但我很确定我做的不对:

function setBooleanForCurrentPhone() {
    var n = 'false';

    var http = new XMLHttpRequest();
    http.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("assignPhone").innerHTML = this.responseText;
        }
    };

    http.open("POST","employee/edit/{id}",true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.send(n);

}

最佳答案

您可以使用 Ajax 来实现这一点。 Ajax 代表异步 JavaScript 和 XML。它将允许您在不刷新页面的情况下提交表单。如果您使用的是 jQuery,那么您可以使用 $ajax 来实现,如果您使用的是 JavaScript,则可以使用 XMLhttprequest 来实现。

关于javascript - 如何在不刷新/提交页面的情况下发送 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555074/

相关文章:

javascript - 如何确保回调仅添加一次?

java - 绘制多个函数时如何消除一组重复的 X 值?

java - 是否可以在 Java 中编辑支持的密码套件列表

javascript - 查找是否在父 div 中选中了所有复选框

javascript - 悬停时缩放 svg map 区域

javascript - Lambda 代理的 AWS APIGateway CORS 不适用

javascript - 使用正则表达式从 HTML 标签属性获取字符串 - JavaScript

javascript - jQuery: $(element).text() 不起作用

java - 使用 Wicket 从 super /抽象类调用方法时焊接注入(inject)失败

html - 在所有设备上垂直居中标题图像,内容不重叠