为什么没有从 Jquery 中的 JSON 中提取该值
我有 2 个名为 Stuno 和 Stuname 的 txt 字段。当我在 txtfield1 中输入学生编号时,它会在 txtfield2 中提取并显示学生姓名。所以这就是我正在执行的文档加载操作,我将焦点放在 txtfield1(stuno) 上。如果我不放置焦点,它会将名称拉得非常好。如果我有焦点,它就不会拉动。表单的加载 我需要关注 txtfield1(stuno)。这是我的代码,如果我做错了什么,请纠正我。
$(document).ready(function(){
$("#stuno").focus();
$("#stuno").change(function(){
if ( jQuery.trim( $( '#stuno' ).val() ) == '' ) {
$('#stuname').val( '' );
$( '#stuerr' ).text( 'Student Account Not Valid!' ) ;
}
else{
$.ajaxSetup ({cache: false});
$.getJSON("student.php",{'stuno' : $("#stuno").attr('value')},
function(data){
if(data[0].status){
var labelvalue = data[0].label.split('-');
$("#stuname").attr("value",labelvalue[1]);
}else{
$('#stuname').val( '' );
$( '#stuerr' ).text(data[0].label);
}
if( $("#stuname").attr("value") ){
$( '#stuerr' ).text( '' ) ;
}
});
}
});
});
}
i have 2 txt fields named stuno and stuname . when i enter student no in the txtfield1 it pulls and shows the student name in the txtfield2. so here is what iam doing onload of the document iam putting the focus on txtfield1(stuno).If i dont put the focus it is pulling the Name very fine . IF i have the focus it is not pulling.Onload of the form
i need to have focus on the txtfield1(stuno).Here is the code which i have please correct me if iam doing something wrong.
$(document).ready(function(){
$("#stuno").focus();
$("#stuno").change(function(){
if ( jQuery.trim( $( '#stuno' ).val() ) == '' ) {
$('#stuname').val( '' );
$( '#stuerr' ).text( 'Student Account Not Valid!' ) ;
}
else{
$.ajaxSetup ({cache: false});
$.getJSON("student.php",{'stuno' : $("#stuno").attr('value')},
function(data){
if(data[0].status){
var labelvalue = data[0].label.split('-');
$("#stuname").attr("value",labelvalue[1]);
}else{
$('#stuname').val( '' );
$( '#stuerr' ).text(data[0].label);
}
if( $("#stuname").attr("value") ){
$( '#stuerr' ).text( '' ) ;
}
});
}
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
keypress
而不是change
。当您离开输入焦点或更改选择框时,会触发更改。只要按下或释放某个键,就会触发 Keypress。所以代替:
使用
Try using
keypress
instead ofchange
. Change is fired when you leave focus of the input or when a select box is changed. Keypress is fired whenever a key is pressed or released.So instead of:
use
并不是你问题的真正答案。
您正在 $ 和 jQuery 之间进行更改,这有点奇怪。另外,您使用
$("#stuno").attr("value") 而不是 $("#stuno").val() 是否有原因?
Not really an answer to your question.
You're changing between $ and jQuery which is a bit weird. Also, is there a reason why you're using
$("#stuno").attr("value") instead of $("#stuno").val()?
有些浏览器在焦点丢失之前不会触发
onChange
事件。也许您想做这样的事情:我已经对您的代码做了一些事情。为了保持一致性,我将所有
.attr('value',...)
调用更改为.val()
。我在准备好的函数外部使用了jQuery
变量,并确保$
变量实际上是准备好的函数内部的 jQuery。我已更改您的change
处理程序以在keypress
上运行 仅当按下的键是 Enter 键时。Some browsers don't fire the
onChange
event until focus is lost. Perhaps you'd want to do something like this instead:I've done a few things to your code. I changed all
.attr('value',...)
calls to.val()
for consistency. And I've used thejQuery
variable on the outside of your ready function and ensure that the$
variable is in fact jQuery inside your ready function. And I've changed yourchange
handler to run onkeypress
only if the key pressed was the Enter key.