使用Spring标记使用jQuery从数组中选择选项

时间:2012-11-30 14:26:52

标签: jquery json spring-mvc

当我使用Spring标签时,选择框的自动填充不起作用。

以下解决方案有效,

    <html>
    <head></head>
    <body>
      <select id="articleLocation">
      </select>
    </body>
    </html>

    /*javascript*/
    var jsonList = {"Table" : [{"stateid" : "2","statename" : "Tamilnadu"},
                {"stateid" : "3","statename" : "Karnataka"},
                {"stateid" : "4","statename" : "Andaman and Nicobar"},
                 {"stateid" : "5","statename" : "Andhra Pradesh"},
                 {"stateid" : "6","statename" : "Arunachal Pradesh"}]}

    $(document).ready(function(){
      var listItems= "";
      for (var i = 0; i < jsonList.Table.length; i++){
        listItems+= "<option value='" + jsonList.Table[i].stateid + "'>" + jsonList.Table[i].statename + "</option>";
      }
      $("#articleLocation").html(listItems);
    });   

下面的解决方案(使用Spring标签)不起作用。错误消息是“''选项'标记只能在有效的'select'标记内使用。”

    <html>
    <head></head>
    <body>
      <form:select path="articleLocation">                                                 
      </form:select>
    </body>
    </html>

    /*javascript*/
    var jsonList = {"Table" : [{"stateid" : "2","statename" : "Tamilnadu"},
                {"stateid" : "3","statename" : "Karnataka"},
                {"stateid" : "4","statename" : "Andaman and Nicobar"},
                 {"stateid" : "5","statename" : "Andhra Pradesh"},
                 {"stateid" : "6","statename" : "Arunachal Pradesh"}]}

    var listItems= "";
    for (var i = 0; i < jsonList.Table.length; i++){
      listItems+= "<form:option value='" + jsonList.Table[i].stateid + "' label='" + jsonList.Table[i].statename + "'/>";
    }
    $("#articleLocation").appendTo(listItems);

0 个答案:

没有答案