【问题标题】:autocomplete error coming in jsp/ajaxjsp/ajax 中出现自动完成错误
【发布时间】:2012-02-27 20:04:19
【问题描述】:

//index.jsp

<html>
 <head>
 <title>JSP page</title>
<script="text/javascript">
$(function() {
// subscribe to the keydown event
$('#text1').keydown(function(e) {
    // when a key is pressed check if its code was 9 (TAB)
    if (e.which == 9) {
        // if TAB was pressed send an AJAX request
        // to the server passing it the currently 
        // entered value in the first textbox
        $.ajax({
            url: '/someservlet/',// i have created a servlet named as someservlet
            type: 'POST',
            data: { value: $(this).val() },
            success: function(result) {
                // when the AJAX succeeds update the value of the 
                // second textbox using the result returned by the server
                // In this example we suppose that the servlet returns
                // the following JSON: {"foo":"bar"}
                $('#text2').val(result.foo);
            }
        });    
    }
});
});
</script>
</head>
<input type="text" id="text1" name="firsttextbox"/>
<input type="text" id="text2" name="secondtextbox"/>
<body>

//做 post() someservlet

String dd = request.getParameter("firsttextbox"); 
String json = new Gson().toJson(options);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

我需要在 head 块中导入任何 js 文件吗?谁能告诉我我错在哪里?在编译时我得到了 jsper...错误。相同的代码在小提琴中工作:

http://jsfiddle.net/JLJ3f/

在 netbeans 中编译时,出现错误。

感谢任何帮助。

【问题讨论】:

    标签: javascript jquery ajax servlets


    【解决方案1】:

    您正在使用 jquery 函数,但您的 html 页面中没有 jquery 库, 在小提琴中,您可以将其导入左侧框架中,在选择的框架菜单下,

    信息:以下是不同的CDN

    所以你可以通过<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>添加它 要么 下载到本地并链接到你的html页面

    【讨论】:

    • 非常感谢您的回答...我会尽快回复您
    • 你能告诉我我应该在 servlet 中做什么修改吗?只需在第一个文本框中输入一些数据,我想自动填充数据来自 servlet 的第二个文本框。
    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 2013-12-03
    • 2018-04-23
    • 2013-08-28
    • 2012-03-29
    • 2019-09-09
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多