sx:autocompleter onchange不调用JQuery函数

时间:2012-07-03 21:55:49

标签: jquery json struts2

我正在使用Struts2 + Spring MVC + Hibernate,我正在尝试创建一个依赖的下拉菜单。但是,我的JQuery函数仅在页面启动时调用,而不是在第一个菜单中选择选项时调用。组织下拉菜单工作正常。以下是相关代码:

search.jsp的

 <%@ taglib prefix="s" uri="/struts-tags" %>
 <%@ taglib prefix="sx" uri="/struts-dojo-tags"%>


<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">  
$(function loadFacilities(value){
   $("#facilitySelect").get(0).options.length = 0;
   $("#facilitySelect").get(0).options[0] = new Option("Loading facilities", "-1");
   $.ajax({
      type: "POST",
      url: "DependentFacility",
      data: "{organizationID:" + value+ "}",
      dataType: "json",
      success: function(msg){
          $("#facilitySelect").get(0).options.length = 0;
          $("#facilitySelect").get(0).options[0] = new Option("Select facility", "-1");

          $.each(msg.d, function(index, item){
             $("#facilitySelect").get(0).options[$("#facilitySelect").get(0).options.length] = new Options (item.Display, item.Value); 
          });
      },
      error: function(){
          $("#facilitySelect").get(0).options.length = 0;
          alert("Failed to load facilities");
      }

   });
 });
 </script> 
 </head>



 <div class="element">
<p class="title"><s:property value="getText('global.a_search_screen')" /></p>
<s:form name="ASearch" action="aSearchList" type="POST">
    <p class="content">
        <table class="topSearch">
        //Some Table stuff was here

              <td >
                    <s:url id="organizationList" action="getListOfOrganizations" />
                    <sx:autocompleter href="%{organizationList}" name="organizations" onchange="loadFacilities(this.value)" keyValue="0" value="---- SELECT ONE-----"/>

                </td>
                <td >
                    <select id="facilitySelect"  name="selectedFacility" ></select>
                </td>

struts.xml中

  <action name="DependentFacility" class="facilityActions">
   <result type="json">
    <param name="root">facility</param>
   </result>
  </action>

FacilityActions.java

public class FacilityActions implements ModelDriven<FacilityVO> {

FacilityService facilityService;
private Map<String, String> facilities = new HashMap<String, String>();
FacilityVO facility = new FacilityVO();

public void setFacilityService(FacilityService facilityService) {
    this.facilityService = facilityService;
}

public FacilityVO getModel(){
    return facility;
}

public String execute() {
    return Action.SUCCESS;
}   

public String addFacility(){
    try{
        facilityService.addFacility(facility);
        return "SUCCESS";}
    catch(Exception e){
        return "ERROR";
    }catch(Throwable t){
        return "ERROR";
    }
}

public Map<String, String> getFacilities() {

    List<FacilityVO> facilityList = facilityService.listFacilities();

    Iterator<FacilityVO> iterator = facilityList.iterator();
    while (iterator.hasNext()) {
        FacilityVO fac = iterator.next();
        facilities.put(fac.getFacilityName(), Integer.toString(fac.getFacilityId()));
    }

    return facilities;
}   

public FacilityVO getFacility(Object thing){
    System.out.println("got a thing:" + thing.toString());
    return facility;
}

public FacilityVO getFacility(){
    System.out.println("Getting facility but nothing given?");
    return facility;
}
}

1 个答案:

答案 0 :(得分:1)

由于Ajax and JavaScript recipes show要设置的相应属性为loadOnTextChange

请记住,这些是Dojo小部件,而不是简单的HTML控件 - 您不一定使用标准HTML属性来修改其行为。

如果您使用的是JSON,请参阅recipe for JSON results;它可以比你制作它简单得多。