jQuery .val()返回未定义的单选按钮 [英] jQuery .val() returns undefined for radio button

查看:91
本文介绍了jQuery .val()返回未定义的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jQuery中获取选中的单选按钮值。但它返回值为'未定义'。我搜索了这个问题的解决方案,但没有为我工作。



我的HTML代码

 < label>< input type =radioclass =servicename =serviceid =all-servicevalue =all-service>所有服务< br><>< br> 
< label>< input type =radioclass =servicename =serviceid =electricianvalue =Electrician>电工< /标签><峰; br>
< label>< input type =radioclass =servicename =serviceid =paintervalue =Painter>画家< /标签><峰; br>
< label>< input type =radioclass =servicename =serviceid =photo and video coveragevalue =Photo and Video Coverage>照片和视频覆盖率< / label>

我的jquery代码..



<$ p $ $($。service:)。$(。service)。change(function(){
alert($(。service:checked ).val());
});
});

这里小提琴为我的问题..

解决方案

在classname和<$ c之间不应该有空格()。$ c>:checked attribute

  $(。service)。 {
alert($(。service:checked)。val());
});

小提琴


I am trying to get the checked radio button value in jQuery. But it returns value as 'undefined'. I searched solution for this issue but nothing works for me.

My HTML Code.

<label><input type="radio" class="service" name="service" id="all-service" value="all-service"> All Service</label><br>
<label><input type="radio" class="service" name="service" id="electrician" value="Electrician"> Electrician</label><br>
<label><input type="radio" class="service" name="service" id="painter" value="Painter"> Painter</label><br>
<label><input type="radio" class="service" name="service" id="photo and video coverage" value="Photo and Video Coverage"> Photo and Video Coverage</label>

My Jquery code..

$(document).ready(function() {
    $(".service").change(function() {
        alert($(".service :checked").val());
    });
});

Here the fiddle for my problem..

解决方案

There should not be any space between classname and :checked attribute

 $(".service").change(function() {
        alert($(".service:checked").val());
    });

Fiddle

这篇关于jQuery .val()返回未定义的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆