下拉式清單N級聯動,下拉聯動

來源:互聯網
上載者:User

下拉式清單N級聯動,下拉聯動
一,後台需要提供以下格式的字串.

       <select style="width: 125px; margin-left: 10px;"><option value="4">動作</option><option value="5">愛情</option><option value="7">恐怖</option></select><select style="width: 125px; margin-left: 10px;"><option value="8">XXX</option><option value="9">XXX</option><option value="10">XXX</option></select><select style="width: 125px; margin-left: 10px;"><option value="11">XXX</option></select>


二,js處理代碼.
                 $(function(){var url = ""; //擷取html的url$("#category_select_div").on("change","select",function(){var parentId = $(this).children('option:selected').val();var se = $(this).nextAll();if(se != null){se.remove();}$.post(url+parentId, function(data) {if(data != null && $.trim(data) != ""){$("#category_select_div").append(data);}var cId = $("#category_select_div select").last().children('option:selected').val();$("#category_id").val(cId);},"html");});});


三,html代碼.
<div id="category_select_div"><select style="width: 125px; margin-left: 10px;"><option value="1">電影</option><option value="2">電視</option><option value="3">綜藝</option></select></div><!-- 儲存ID值的input --><div><input id="category_id" type="hidden"/></div>

四,表結構如下:id,parent_id,name
1    0      電影
2    0      電視
3    0      綜藝
4    1      動作
5    1      愛情
6    1      戰爭
7    1      恐怖
......



五,幕後處理代碼:1,查詢sql語句:select id,parent_id,name from category t where t.parent_id = 1
2,擷取到的是一個List<Category> list;
3,

Integer parentId = 1;PrintWriter out = response.getWriter();StringBuffer html = null;List<Category> list = null;html = new StringBuffer("");do{list = categoryServices.selectListByParentId(parentId);//根據上面的sql語句查詢listif(list.size()>0){parentId = list.get(0).getId();for (int i = 0; i < list.size(); i++) {if(i==0){html.append("<select style=\"width: 125px;margin-left: 10px;\">");}html.append("<option value=\""+list.get(i).getId()+"\">"+list.get(i).getName()+"</option>");if(i==list.size()-1){html.append("</select>");}}}}while(list.size()>0);out.print(html.toString());out.flush();out.close();





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.