【问题标题】:How to pass onchange value from form:select into controller如何从表单传递 onchange 值:选择到控制器
【发布时间】:2019-11-01 18:03:17
【问题描述】:

我正在使用 Spring MVC 来开发 Java Web 应用程序。

在我目前的情况下,我陷入了一种情况。

在我当前的项目中,我有一个下拉菜单,我需要将 onChange 上的选定值传递给控制器​​以处理结果。

为了显示下拉菜单,我使用 form:select (Spring Form)。

您能否建议我如何在 OnChange

上将选定的值传递给控制器
<body>
<form:form method="post" modelAttribute="ad" action="/save">
    <form:select cssClass="select" cssStyle="width:100%;margin-left:10%;"
        path="work_type" items="${allworktype}" itemValue="id"
        itemLabel="work_type" />
</form:form>

    @Controller
public class WebController {

    @Autowired
    public AdminBuildingRepo adminBuildingRepo;

    @ModelAttribute("allworktype")
    public List<AdminBuilding> getblocks(Model model){
    return  adminBuildingRepo.findAll();
    }


    @GetMapping("/")
    public String home(Model model) {
        AdminBuilding ad= new AdminBuilding();
        model.addAttribute("ad", ad);
        return "index";
    }



    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public String transferForDevice( Model model) throws Exception {
        System.out.println("*********");
        //so now I can use "user" from @ModelAttribute
        return "redirect:/admin";
}

}
  • 请帮助我如何在选择时将值传递给控制器​​。

【问题讨论】:

    标签: spring spring-boot spring-mvc spring-data-jpa


    【解决方案1】:
    function formSubmit(){ 
    
      $('form#myForm').attr({action: 'save'});  
      $('form#myForm').attr({modelAttribute: 'ad'});  
      $('form#myForm').attr({method: 'post'});    
      $('form#myForm').submit();
     }
    

    用 id myForm 声明表单 在 select onchange 上调用这个函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2021-09-02
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多