绑定点击 jquery 时出错检查值为空
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').bind('click', function(){
var option = $('#option1').val();
if(option == ''){
alert('Answer empty');
}
return false;
});
});
</script>
……
<input type="radio" id="option1" name="option1 />
<input type="submit" name="btnSubmit" id="btnSubmit" value="submit" />
=>错误点击按钮提交不提醒
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').bind('click', function(){
var option = $('#option1').val();
if(option == ''){
alert('Answer empty');
}
return false;
});
});
</script>
...
<input type="radio" id="option1" name="option1 />
<input type="submit" name="btnSubmit" id="btnSubmit" value="submit" />
...
=> error click button submit not alert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于该无线电的值是
on
(默认值),请查看此处:http://jsfiddle .net/DV9WP/Because the value of that radio is
on
(default value), look here: http://jsfiddle.net/DV9WP/您应该稍微修改一下代码,获取
:checked
的值单选按钮,如果未选中,则意味着该值为空,并警告答案为空,例如
这里是工作示例 http://jsfiddle.net/L8VY8/
you should modify you code a bit get the value of
:checked
radio button and if its not checked it means the value is empty and alert the answer is emptyfor example
here is the working example http://jsfiddle.net/L8VY8/