0

我想要一个下拉列表,当我更改第一个下拉列表时会发生变化。我确定我的 servlet 是正确的,因为它返回了我需要的值,但是我的 servlet 没有运行。当我调试时,它没有命中 servlet 中的断点。

我认为这与我的javascript有关。

这是JSP文件:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<SCRIPT type="text/javascript" src="dropDownOnderdelen.js"></script>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
 Select a file to upload: <br />
<form action="/Racing/UploadServlet" method="post"
                    enctype="multipart/form-data">
 <input type="file" name="file"  />
<br />
Logische Naam: <input type="text" name="logicalName"><br>
Stuknr(automatisch,nog niet geimplementeerd): <input type="text" name="partNr"><br>
Kost: <input type="text" name="cost"><br>


Assembly:
<select name = "assembly">
 <c:forEach var ="assembly" items="${Assemblys}">
 <option value="${assembly.id}">${assembly.name}</option>
 </c:forEach>
 </select>
 <select id="onderdeel">
 <option>Kies eerst een assembly</option>
 </select>
 <br>
  <textarea name="comments" cols="25" rows="5">
  Verdediging Design
  </textarea><br>
  <input type="submit" value="Upload File" />
 </form>
 </body>
 </html>

这是javascript文件

 $(document).ready(function() {
   $('#assembly').change(function() {
    var selectedValue = $(this).val();
    var servletUrl = 'OnderdelenServlet?value=' + selectedValue;

    $.getJSON(servletUrl, function(options) {
        var dropdown2 = $('#onderdeel');
        $('>option', dropdown2).remove(); // Clean old options first.
        if (options) {
            $.each(opts, function(key, value) {
                dropdown2.append($('<option/>').val(key).text(value));
            });
        } else {
            dropdown2.append($('<option/>').text("Please select dropdown1"));
        }
    });
  });
});

servlet 没问题,我很确定,所以没必要把它放在这里。

4

2 回答 2

2

You have used

$('#assembly').change ===> # stands for ID (element ID)

in your javascript

where as in your JSP file you have < select> as < select name = "assembly">

so assign "ID" as well to you < select> box < select name = "assembly" id="assembly">

于 2013-02-01T06:49:50.840 回答
0

在同样的情况下我在做什么

纪律是我的第一个组合ID;

$("#discipline").change(function() {
         var data = $("#discipline option:selected").text();

                $.ajax({
                      type: 'POST',
                      url: 'registerdcodes?type=discipline&discipline='+data,
                      success:function(data){  
                    fillComboBox(data,$("#codes"));//populating secondcombo
                      }
                    });

     });

还添加了 fillcombo 方法:

如果您需要“请选择您的选项”作为第一个项目通过isSelectRequired true

而 villlagesString 是由 | 操作的 bigString

// 例如:villagesString 应该是这样的 .. villagesString ="option1|option2|option3|option4";

    function fillComboBox(villagesStrings,box,isSelctRequired){
        if(villagesStrings !=null){
            var villagesStrings = villagesStrings.split('|');
            if(isSelctRequired){
            box.append("<option value='null'>Please select your option</option>");
            }
            for (var i = 0, l = villagesStrings.length-1; i <l; i++){
                var id =villagesStrings[i].substring(0,2);
                var qtn =villagesStrings[i].substring(0,villagesStrings[i].length)
                box.append("<option value="+qtn+">"+qtn+"</option>");
        }
        }

        }
于 2013-02-01T07:24:15.403 回答