gpt4 book ai didi

javascript - 从ajax调用获取值后如何在if条件中打印成功的语句?

转载 作者:行者123 更新时间:2023-12-02 11:25:17 24 4
gpt4 key购买 nike

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").change(function(){
var p1 = $('#password1').val();
var p2 = $('#password2').val();
var p3 = $('#password3').val();
var p4 = $('#password4').val();

$.post('http://localhost../mycode.jsp',
{
pass1 : p1,
pass2 : p2,
pass3 : p3,
pass4 : p4,
},
function(data,status){
console.log(data)
alert("Status : "+ status);
});
});
});
</script>
</head>
<body>
<%
String pw1,pw2,pw3,pw4;
pw1 = "1";
pw2 = "2";
pw3 = "3";
pw4 = "4";
out.println("<div id='password' style=' display: block; position: absolute; left: 30%;'>");
out.println("<h1 style=' position: absolute; top: -120%; left: 18%; font-family: Titillium Web;'>PASSWORD</h1>");
out.println("<form method='POST'>");
out.println("&nbsp;&nbsp;&nbsp;&nbsp; <input type='number' id='password1' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println("&nbsp;&nbsp;&nbsp;&nbsp; <input type='number' id='password2' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println("&nbsp;&nbsp;&nbsp;&nbsp; <input type='number' id='password3' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println("&nbsp;&nbsp;&nbsp;&nbsp; <input type='number' id='password4' maxlength='1' max='9' style=' width: 1%; padding: 12px 12px; margin: 8px 0px; border: none; border-bottom: 2px solid red;'>");
out.println("</form>");
out.println("</div>");

String ele1 = request.getParameter("pass1");
String ele2 = request.getParameter("pass2");
String ele3 = request.getParameter("pass3");
String ele4 = request.getParameter("pass4");

if((pw1.equals(ele1)) && (pw2.equals(ele2)) &&(pw3.equals(ele3)) && (pw4.equals(ele4)))
out.println("Code Successful!!");
else
out.println("Code Failed.");
%>
</body>
</html>

这是我正在使用的基本代码。我想在用户表单数据异步发送到服务器并且服务器执行任务后执行操作。如何更新那些由用户在运行时设置的 if 条件值。

最佳答案

I want to perform an action after the user form data is sent asynchronously to the server and server performs tasks.

假设我们坚持使用 Jquery ajax,您想要响应异步请求而执行的任何操作都必须通过回调函数完成

换句话说:

$.post('http://localhost../mycode.jsp',
// ... other arguments to ajax call
function(data,status){
// do stuff that requires waiting on async request HERE
});

这是您知道请求已完成(无论状态如何)的唯一地方。

您可以将 if 逻辑移到那里,或者另一个建议是将您想要执行的逻辑包装到另一个较小的函数中。然后从 $.post 回调内部调用它。

关于javascript - 从ajax调用获取值后如何在if条件中打印成功的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com