0

我是 ajax 新手。我想将选择框的参数值重定向到 servlet 到我的 servlet。这是我的代码,当我使用 request.getParameter("type") 时它没有检索值,它给了我一个空值。

     <script>
       $(document).ready(function() {       
         $('#type').change(function() {                              
           $.get('pickasset', function(responseJson) {
             var type = $('#type').val();
            $.ajax({
                type:'POST',
                url: 'PickAssetServlet',
                data: type          
            }); 
           });
        });
      });
    </script>

    <form action="pickasset" method="post">
        <select id="type" name="type">
           <option value="Non-Sap">Non Sap</option>
           <option value="Sap">Sap</option>
        </select>
    </form>

当我更改选择框时,它必须转到 servlet 并在那里执行逻辑。

4

3 回答 3

0

Check box value must be obtained from checkbox in the right way, as mentioned in jQuery docs (http://api.jquery.com/val/), just pay attention to multiple select, you're gonna have an array of values. You are doing it right, according to docs.

var varValue = var type = $('#type').val();

Then sending data as 'json' you will be able to read them with request.getParameter('type')

data: { "type" : varValue }

If you still get null, try to check if your "type" param and his value are in the request (chrome request inspector will be usefull, then check it in the debugger).

.

P.S.

Just check if you have something in your webapp filterchain that may wrap you request or hide some params, in some large web-app you get lost very easly.

于 2013-07-10T16:42:53.567 回答
0

如果您没有从选择字段中获取选定的选项,则需要使用 $('#type').find(":selected").text()来检索选定的选项。val()不适用于选择字段。也按照 pXL 所说的去做,因为这就是您使用 jQuery ajax 方法发送数据的方式

于 2013-07-10T15:42:42.337 回答
0

像这样发送您的数据-

data: { type : type }
于 2013-07-10T15:27:51.387 回答