Javascript 向服务器提交Jquery按钮的值
我有一个带有多个按钮的表单,我想使用脚本将表单提交给服务器,因此我使用一个绑定到按钮单击事件的函数。在服务器端,我希望能够获取单击的按钮的值。如何做到这一点 使用传统的方法发布表单,我可以执行request.getParameter()并获取单击的按钮的值。我还想在服务器端执行request.getParameter()并获取此参数或其他更有效的方法。下面是我所拥有的: jqueryJavascript 向服务器提交Jquery按钮的值,javascript,jquery,html,jsp,Javascript,Jquery,Html,Jsp,我有一个带有多个按钮的表单,我想使用脚本将表单提交给服务器,因此我使用一个绑定到按钮单击事件的函数。在服务器端,我希望能够获取单击的按钮的值。如何做到这一点 使用传统的方法发布表单,我可以执行request.getParameter()并获取单击的按钮的值。我还想在服务器端执行request.getParameter()并获取此参数或其他更有效的方法。下面是我所拥有的: jquery function submitPage(){ document.getElementById
function submitPage(){
document.getElementById("citizenRegistration").action="citizen_registration.htm";
document.getElementById("citizenRegistration").target="_self";
document.getElementById("citizenRegistration").method = "POST";
document.getElementById("citizenRegistration").submit();
}
$(document).ready(function(){
$('#query').click(function(e){
e.preventDefault();
//need to send the value of the button to server
alert($(this).val());
//alert("hello");
submitPage();
});
});
Jsp
<li><input id="save" type="submit" name= "user_request" value="Save"/>
<input id="update" type="submit" name= "user_request" value="Update"/>
<input id="query" type="submit" name= "user_request" value="Query"/>
</li>
您可以将按钮值发送到函数
$('#query').click(function(e){
e.preventDefault();
//need to send the value of the button to server
var buttonVal = $(this).val();
alert(buttonVal);
submitPage();
});
希望这对您有所帮助。可以使用查询字符串发送。您忘记将buttonVal传递到submitPage()中了在click函数中,感谢您的回答,您也提供了帮助。您向我展示的这种方法允许通过url传递参数。我还介绍了一种方法,在单击按钮时,我使用jquery创建了一个隐藏的输入元素,将值和名称属性从按钮传递到隐藏的元素。然后将其发布到服务器。你的方法也很简单,只是对控制器进行一个小的调整,以接受参数,而不是请求参数。我现在有更多的问题。你能帮我看看吗?我需要一些建议,这里很紧急
$('#query').click(function(e){
e.preventDefault();
//need to send the value of the button to server
var buttonVal = $(this).val();
alert(buttonVal);
submitPage();
});