问题陈述: 当我们在文本框中输入时,它必须显示在index.html文件的文本字段中输入的完全相同的文本,而不必点击使用Ajax提交按钮
为此,我用以下方式编码:
的index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Submit after typing finished</title>
<script src="jquery-3.2.1.min.js"></script>
<script>
function myFunction() {
$('#search-input').keyup(function (event) {
var query =($('#search-input').val());
if (query != '' || query != ' ') {
$.ajax({
type: 'GET',
url: "/ajaxmssql/XViews.py",
data: {
'q': query
},
dataType: "html",
success: function(data)
{
var params = $(html).filter(function(){ return $(this).is('p') });
params.each(
function()
{
var value = "<li>" + $(this).html() + "</li>";
$("#main-results-search").append( value );
}
);
},
error: function(request, ajaxOptions, thrownError)
{
$("#debug").html(request.responseText);
$("#debug").html("Could Not Print from Server Side Script");
}
});
}
});
}
</script>
</head>
<body>
<h1>Printing...</h1>
<input type="search" onkeyup="myFunction()" id="search-input" placeholder="Type to print ..."/>
<div id="main-results-search"></div>
<div id = "debug"></div>
</body>
</html>
XViews.py
#!C:/Users/user/AppData/Local/Programs/Python/Python36/python.exe
import cgi
import cgitb
cgitb.enable()
def print_header():
print("""Content-type: text/html\n
<!DOCTYPE html>
<html>
<body>""")
def print_close():
print ("""</body>
</html>""")
def display_error():
print_header()
print ("<p>An Error occurred parsing the parameters passed to this script.</p>")
print_close()
def main():
form = cgi.FieldStorage()
search = form.getvalue('search-input')
query = request.GET.get('q')
if query is not None and query != '' and request.is_ajax():
print ("<p>This is from query</p>")
if search is not None and search != '' and request.is_ajax():
print ("<p>This is from search</p>")
# if "search" in form:
# print_header()
# print("<h2>Entered Text %s %s</h2>" % (search))
# print_close()
# else:
# display_error()
main()
输出结果:但每当我输入任何文字字符时,它会显示无法打印而不是预期结果?