2014-01-09 105 views
0

我正在创建一个Web应用程序,我想通过单选按钮完成选择控制器。现在,我希望此操作被执行而无需提交按钮。我GOOGLE了很多,但没有提交按钮就没有办法做到这一点。命中Servlet与单选按钮选择无需提交按钮

这里是HTML代码这一要求

<form action="${pageContext.request.contextPath}/env" method="post"> 
    QA71<input type="radio" name="env" value="QA71" checked="checked"> 
    QA72<input type="radio" name="env" value="QA72" > 
    </form> 

这里是Java这一要求

@RequestMapping(value = "/env", method = RequestMethod.POST) 
    public String env(HttpServletRequest request){ 

     logger.info("parameter is "+request.getParameter("env")); 
     return "home"; 

    } 

我感谢你的帮助的代码。

+0

您可以通过javascript/ajax发送值。 –

+0

你能为我提供代码吗? –

+0

当然,你可以检查这个[链接](http://fruzenshtein.com/spring-mvc-ajax-jquery/),会给你更好的主意。 –

回答

0

如果你不想使用一个提交按钮,你应该使用JavaScript提交表单做的一个方法是:

document.getElementById("myForm").submit(); 

所以首先你需要把一个id属性到您的表单,然后你需要决定在这个例子中,我想选择什么样的事件来提交表单:

<form id="myForm" action="${pageContext.request.contextPath}/env" method="post"> 
    QA71<input type="radio" name="env" value="QA71" checked="checked"> 
    QA72<input type="radio" name="env" value="QA72" onBlur="submitForm()"> 
</form> 

function submitForm() { 
    document.getElementById("myForm").submit(); 
} 
+0

感谢您的回答。 –