Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

javascript - Can you use an ajax function to send a variable from jsp to a servlet and open it at the same time without a form?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the 'form' in which I use to make the inputs.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TextArea Input</title>
</head>
<body>

<form>
Input Area: Format for input = FirstName,LastName,8DigitIDNum (Comma included)
<br/>
<TEXTAREA id = "textarea" COLS = "40" ROWS = "10" onkeypress = "arraybuild(event, this)"></TEXTAREA>
<br/>
<br/>
<input type="button" id="submit" value="Submit" disabled = "true" onclick="send(this)"/>
<br/> <br/>
<input type="button" value="Reset" onclick = "unlock(this)"/>
<br/> <br/>
</form>

<script type="text/javascript">

This is the main code, its a function for a text area that makes an array input every time I press enter. The finalstring is supposed to be the array compressed into a string for me to send to the servlet.

var input;
var uneditstring;
var editremovespace;
var newstrings;

function arraybuild(e){
    var input = (e.keyCode ? e.keyCode : e.which);
    if (input == 13){//Note: key number 13 is Enter Key
        uneditstring = document.getElementById("textarea").value;
        editremovespace = uneditstring.split(" ").join("");
        newstrings = editremovespace.replace(/
/g, ",").split(",");
        console.log(newstrings)
        if(newstrings.includes('-1')){
        newstrings.pop();
        console.log(newstrings)
            if(checkValues(newstrings)){
                if(checkID(newstrings)){
                    if(checkNames(newstrings)){
                        document.getElementById("textarea").readOnly = true;
                        document.getElementById("submit").disabled = false;
                        alert('You entered -1, you can now submit the inputted values.')
                        alert('If you want to reopen the text area, click Reset.')
                    }else{
                        alert('ERROR: Names are not valid.');
                        alert('ERROR: Due to containing a number or being over 255 characters.');
                    }
                }else{
                    alert('ERROR: One of the IDs is not Valid.');
                    alert('ERROR: Due to starting with a double zero, 
having a letter in them, 
or being longer than 8 digits.');
                }
            }else{
                alert('ERROR: Note enough values inputted. At least 3 Values are required.');
            }
        }else{
            console.log('Not yet pressed -1');
        }
    }
}

Neither of these codes have given me any errors at all. PS: Apologies if it is still too long.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...