JS dynamic creation of combobox, js Dynamic combobox

Source: Internet
Author: User

JS dynamic creation of combobox, js Dynamic combobox

I have not written JavaScript code in my usual learning process. Because I am not familiar with it, I am more or less afraid of JS Code. When I arrived at the company, the overwhelming JS Code was simply dumbfounded. In the world where JavaScript code is used all over the world, I have learned a little bit about it. I would like to warm up with a small feature:

  • Use JS Code to dynamically create a comboboxThe function is simple. ajax is used to asynchronously obtain data from the background and display the data on the foreground. How can I write the specific JS Code? How do I write the background code?

Js:

<span style="font-size:18px;">$dialog.find("select[name=materialunitid]").change(function() {if ($(this).val()) {$dialog.find("select[name=productid]").removeAttr("disabled");$.ajax({url: contextPath + "/jobcontent/getproductsbyunitid.do?unitid=" + $(this).val() + "&t=" + $.now(),async: false,dataType: "json",success: function(data) {var $select = $dialog.find("select[name=productid]").empty();$select.append(new Option("Select...", ""));if (data && data.length) {$.each(data, function(i, t) {$select.append(new Option(t.productname, t.id));});}}});} }</span>

Use the find () method to obtain the descendant of each element in the current Element Set, filter by selector, jQuery object, or element, and use ajax to dynamically obtain the data to be displayed from the background; use the append method to dynamically add an Option.

 

Action:

<span style="font-size:18px;">@RequestMapping("/getproductsbyunitid.do")@ResponseBody@Overridepublic Object getProductsByUnitId(HttpServletRequest request) {String unitId = request.getParameter("unitid");List<DataProducts> results = jobContentService.queryEntities(DataProducts.class, null, DataProducts.PROP_MATERIALUNITID+ " = '" + unitId + "'", null, null, null);if (results != null && results.size() > 0) {return JSONArray.fromObject(results).toString();}return "[]";}</span>

Use HttpServletRequest to obtain the Id from the foreground. query the data in the database based on the Id. Convert the data into a JSON string and pass it to the foreground.

 

  • The javascript code control drop-down box cannot be Edited:

Method 1:

<span style="font-size:18px;">$('#cc').combobox({    required:true,    multiple:true,disabled:true  }); </span>

Method 2:

<span style="font-size:18px;">$('#cdeptno').combobox("disable");</span>
  • Dynamically Retrieve the selected value from the drop-down list:
<span style="font-size:14px;">var unitname = $dialog.find("select[name=materialunitid] option:selected").text();</span>

Summary:

Writing JS Code is actually not much different from writing back-end code. Clarify your ideas and follow your ideas. pay more attention to the specific methods of getting it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.