键码值发送jquery
一切都很顺利,只是有点错误,只有
当我按 Enter 键时,它才会通过 $.post() 发送值。成功后,页面刷新,为什么会这样
,如果我使用其他一些键码,它将保持插入的相同值,但不会刷新
<script>
$(document).ready(function(){
$('#newNumber').focus();
$('#newNumber').keypress(function(e){
var code=e.keyCode;
//alert(code);
if(code==61)
{
var nno=$('#newNumber').val();
$('#load').show();
if(nno=="")
{
$('#load').hide();
$('#alertBox').show();
$('#newNumber').focus();
$('#alertBox').html("<div class=error><span id=alerttext class='alerttext'>Empty Field</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else
{
$.post("NumberAction/addAction.php",$('#contentForm').serialize(),function(result){
if(result=="yes")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=warning><span id=alerttext class='alerttext'>Already Exists</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else if(result=="done")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=success><span id=alerttext class='alerttext'>New number is added</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else if(result=="error")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=error><span id=alerttext class='alerttext'>Error in adding</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
})
}
}
})
})
</script>
现在值正在运行,但当我按 Enter 时,它会发送该值,但也会刷新页面,这样我就无法使用 Enter 键代码
Everything is going fine jst a little bit mistake only
when i press enter it will send the value througn $.post(). after success the page refreshes why so
and if i use some other keycode it will remain same value inserted but no refresh
<script>
$(document).ready(function(){
$('#newNumber').focus();
$('#newNumber').keypress(function(e){
var code=e.keyCode;
//alert(code);
if(code==61)
{
var nno=$('#newNumber').val();
$('#load').show();
if(nno=="")
{
$('#load').hide();
$('#alertBox').show();
$('#newNumber').focus();
$('#alertBox').html("<div class=error><span id=alerttext class='alerttext'>Empty Field</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else
{
$.post("NumberAction/addAction.php",$('#contentForm').serialize(),function(result){
if(result=="yes")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=warning><span id=alerttext class='alerttext'>Already Exists</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else if(result=="done")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=success><span id=alerttext class='alerttext'>New number is added</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
else if(result=="error")
{
$('#load').hide();
$('#alertBox').show();
$('#alertBox').html("<div class=error><span id=alerttext class='alerttext'>Error in adding</span><div id=close class='close'><img src='images/close.png'/></div></div>");
$('#newNumber').val("");
$('#newNumber').focus();
$('#close').click(function(){$('#alertBox').fadeOut(2000);});
}
})
}
}
})
})
</script>
Now values is going but when i press enter it will send the value but also it will refresh the page so that i am not able to use the Enter key code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您正在 FORM 标记内的某处检测到按下的键,并且通过按 Enter 您不仅使用 $.post 发送 ajax,而且还告诉浏览器以常规方式提交表单,以便页面重新加载。为了防止这种情况,您可以执行以下两种操作之一:
1)防止提交表单,使用:
2)执行“$.post(“NumberAction/addAction.php”,....”后,返回 false ;
My guess is that you are detecting pressed key somewhere inside FORM tag and by pressing enter you not only send the ajax using $.post, but also tell the browser to submit the form regular way so the page is reloaded. To prevent this, you can do one of two things:
1) Prevent submission of the form, using:
2) After doing "$.post("NumberAction/addAction.php",...." do return false;