1

我有一个jsp,其中我有select标签,我想从我的Servlet中的jsp中的select中获取和选择的值

<select id="listoffood" name="dropdown" onchange="foodname();">
<option value="bg">Burger</option>
<option value="pas">pasta</option>
<option value="pi">pizza</option>
</select>
<div id='content'></div>

这是javascript代码

function foodname()
{

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };

}

我所做的是使用了这样的获取属性,但它无法正常工作,显示为空

protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException 

    {
    // TODO Auto-generated method stub

    String coun = request.getParameter("dropdown");
    PrintWriter out=response.getWriter();
    System.out.println("here : "+coun);
}

提前致谢,任何一段代码都非常感谢。

4

5 回答 5

1

只需将您的 AJAXopen()请求更改为

var select = document.getElementById("listoffood");
self.xmlHttpReq.open('GET', "InformationServlet?dropdown=" + select.options[select.selectedIndex].value, true);
于 2013-05-03T11:49:19.613 回答
0

使用方法getParameterValues(String)
这是因为<select>标签可以有多个选定的值(例如select multiple

String[] coun = request.getParameterValues("dropdown");
于 2013-05-03T11:00:33.050 回答
0

检查这篇文章:

如何在 JSP 页面的选项标签上使用 onClick() 或 onSelect()?

您似乎正在使用 select 标记而没有任何操作,例如 onchange

于 2013-05-03T11:01:15.353 回答
0

您可以使用从列表框中获取选定的项目文本

var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value

在您的 JS 方法中并使用 servlet 的链接发送此值。

于 2013-05-03T11:03:43.753 回答
0

从选择标签中获取值

var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value;

和编辑js函数

self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);

最后你应该看起来像

function foodname()
{

    var e = document.getElementById("dropdown");
    var selectedValue = e.options[selectBox.selectedIndex].value;

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };
}
于 2013-05-03T11:23:04.210 回答