关于JQUERY的问题,不能得到值,求解!

cqheshuang 2010-11-11 01:51:39

<form action='<?php echo url('default/diary')?>' method='post'>
<div id='cmset'>
<p><span class='marginleft'>标题:</span><span><input class='inputsize' type='text' name='title'><font color='red'>(*)</font></span><span class='titlerror'></span></p>
<p><span class='marginleft'>昵称:</span><span><input class='inputsize' type='text' name='nickname'><font color='red'>(*)</font></span><span class='nickerror'></span></p>
<p><span class='marginleft'>邮箱:</span><span><input class='inputsize' type='text' name='email'><font color='red'>(*)</font></span><span class='emailerror'></span></p>
<p><span>个人主页:</span><span><input class='inputsize' type='text' name='weburl'></span></p>
<p><span>留言内容:</span><textarea rows='4' cols='26' name='content' style="margin-left: 5px; vertical-align: text-top;"></textarea><font color='red'>(*)</font><span class='conerror'></span></p>
<p><span style='margin-left: 55px;'><input type='submit' value='提交'></span>
</div>
</form>
<div class='commentlist'>

</div>

<script type='text/javascript'>
$(document).ready(function(){
$title = $(":input[name='title']").val();
$nickname = $(":input[name='nickname']").val();
$email = $(":input[name='email']").val();
$content = $(":input[name='content']").val();
$(":input[name='title']").blur(function(){
if($title==''){
alert($title); ------------------------------------------------------------------->这里出现的是空值
$('.titlerror').html("<font color='red'>标题不能为空</font>");
}else{
$('.titlerror').html('');
}
});
$("input[name='nickname']").blur(function(){
if($nickname==''){
$('.nickerror').html("<font color='red'>名字不能为空</font>");
}else{
$('.nickerror').html("");
}
});
$("input[name='email']").blur(function(){
if($email==''){
$('.emailerror').html("<font color='red'>邮箱不能为空</font>");
}else{
$('.emailerror').html("");
}
});
$("input[name='content']").blur(function(){
if($content==''){
$('.conerror').html("<font color='red'>内容不能为空</font>");
}else{
$('.conerror').html("");
}
});
});
</script>

我好多方法都试过了,就是得不到文本框里面的值。。。。怎么回事?
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
热心男孩 2011-06-27
  • 打赏
  • 举报
回复
请问一下那到底要怎么写呀?
persuit666 2010-11-11
  • 打赏
  • 举报
回复
真搞啊,if($title==''){
alert($title); ------------------------------------------------------------------->这里出现的是空值
你的判断就是$title=='',如果能执行的话,它不为空是啥?
cqheshuang 2010-11-11
  • 打赏
  • 举报
回复
非常感谢,我刚才思路搞乱了。。。
kongxh1218 2010-11-11
  • 打赏
  • 举报
回复
你在页面加载完成的时候执行了这样的代码:$title = $(":input[name='title']").val();
在blur事件里只会把变量$title的值拿来比较,不会执行取值的代码,所以$title永远都是空。打个比方吧
var a = 1;
var b = 2;
var c = a + b;
a = 2;
alert(c);
你能指望弹出4来吗。

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧