[英]prevent page reload and call a jquery function when submit button is clicked
The form tag contents are:- 表单标签的内容是:-
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ;?>" >
The button tag contents are :- 按钮标签的内容是:
<input type="submit" id="submit_button" value="Submit">
The j-query functions are j-query函数是
$('#submit_button').click(function ()
{
alert("button clicked");
buildingVal = $("#building").val();
levelVal = $("#level").val();
data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
$.ajax(
{
url: "res.php",
type: "POST",
data: data,
success: function (data) {
}
});
return false;
});
The page is getting reloaded, and the data in the textboxes and dropdown menus are disappearing. 该页面正在重新加载,文本框和下拉菜单中的数据正在消失。
but if i have only this code in the jquery:- 但是如果我在jQuery中只有这个代码:
$('#submit_button').click(function ()
{
alert("button clicked");
return false;
});
then the page doesn't reload and the values remains intact. 那么该页面不会重新加载,并且值保持不变。
Please could you tell me how am i to prevent the page from reloading? 请您告诉我如何防止页面重新加载?
Also in the ajax call i will be calling a page res.php which will return a table, 同样在ajax调用中,我将调用res.php页面,该页面将返回一个表,
What will the code be in the 代码将在
success: function (data) {
}
please help... 请帮忙...
Edit: 编辑:
I am passing the data into the res.php page with the code 我使用代码将数据传递到res.php页面
data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
and then pass it into the page using 然后使用
$.ajax(
{
url: "res.php",
type: "POST",
data: data,
success: function (data) {
$('#npc').html(data);
}
});
in the res.php page 在res.php页面中
how do i extract the two values from the single value that was passed 我如何从传递的单个值中提取两个值
i have tried using the following code 我尝试使用以下代码
$building = mysql_real_escape_string($_GET['building']);
$level = mysql_real_escape_string($_GET['level']);
But it doesn't work... 但这行不通...
You have an error in jQuery code: jQuery代码中有一个错误:
Error: 错误:
buildingVal = $("#building").val();
levelVal = $("#level").val();
data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
Solution: 解:
buildingVal = $("#building");
levelVal = $("#level");
data = 'building=' + buildingVal.val() + '&level=' + levelVal.val();
Complete code js: 完整的代码js:
$('#submit_button').click(function () {
var
buildingVal = $("#building"),
levelVal = $("#level"),
data = 'building=' + buildingVal.val() + '&level=' + levelVal.val();
$.ajax({
'url': 'res.php',
'type': 'POST',
'data': data,
'success': function (data) {
}
});
return false;
});
Edit 编辑
If your ever going to use this form to send data by ajax, the best way is to cancel the event "submit" the form: 如果您打算使用此表单通过ajax发送数据,最好的方法是取消事件“提交”表单:
HTML: HTML:
<form id="myform" method="post" action="<?php echo $_SERVER['PHP_SELF'] ;?>" >
...
</form>
JS: JS:
$('#myform').bind('submit', function(event) {
return false;
});
$('#submit_button').bind('click', function () {
var
buildingVal = $("#building"),
levelVal = $("#level"),
data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
$.ajax({
'url': 'res.php',
'type': 'POST',
'data': data,
'success': function (data) {
}
});
});
Best way is to not use a submit
button in the first place. 最好的方法是首先不要使用submit
按钮。
<input type="button" id="myButton" value="Submit">
$('#myButton').on('click', function(){
//do ajax
});
You need to prevent the default behavior in your click event: 您需要防止点击事件中的默认行为:
$('#submit_button').click(function (event)
{
event.preventDefault();
alert("button clicked");
return false; // Not truly necerssary
});
http://api.jquery.com/event.preventDefault/ http://api.jquery.com/event.preventDefault/
So in order for the submit button to not submit your page with its default behavior do the following: 因此,为了使提交按钮不以默认行为提交您的页面,请执行以下操作:
$('#submit_button').click(function (event) {
event.preventDefault();
buildingVal = $("#building").val();
levelVal = $("#level").val();
data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
$.ajax(
{
url: "res.php",
type: "POST",
data: data,
success: function (data) {
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.