简体   繁体   中英

combobox jquery onchange event

How i update combobox based on another combobox, i have this code but its not work, can someone help me. Thanks

  <select name="marca" id="marca" onchange="javascript:carregaModelos(this.value)" >
    <option th:each="marca : ${marcas}" 
      th:value="${marca.idmarca}" 
      th:text="${marca.nomemarca}">Marca</option>
</select>

     <select   name="modelo" id="modelo">
  <option th:each="modelo : ${modelos}" 
      th:value="${idmodelo}" 
      th:text="${nomemodelo}">Modelo</option>
     </select>


<script type="text/javascript">     
   function carregaModelos(marca) { 
 var opcao = $(this).('#marca option')   
 console.log(opcao);
 jQuery("#modelo").load( "pesquisa/" + opcao);
 return false;


}

and this

 @RequestMapping("/pesquisa/{idmarca}")
    public String pesquisa(ModelMap model, @PathVariable Long idmarca) {
        model.addAttribute("modelo", service.obterModelosByMarcas(idmarca));
        return "index";

Add an attribute with value to target element.

data-target-sync="#targetElementId"

The value is jQuery selector so # included with ID of target element

 $(document).ready(function() { $('[data-target-sync]').change(function() { var cur = $(this); $(cur.attr('data-target-sync')).val(cur.val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select name="marca" id="marca" data-target-sync="#modelo"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> <select name="modelo" data-target-sync="#marca" id="modelo"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM