jquery星级评分得到价值无线电隐藏

时间:2012-10-26 14:51:35

标签: jquery

我使用星级http://www.fyneworks.com/jquery/star-rating/

我不明白在提交表格后如何获取用户检查的隐藏收音机的价值

这里是代码:

         <div class="line mrgT mrgB" id="starVin">
            <input name="star1" type="radio" class="star" value="1" />
            <input name="star1" type="radio" class="star" value="2" />
            <input name="star1" type="radio" class="star" value="3" />
            <input name="star1" type="radio" class="star" value="4" />
            <input name="star1" type="radio" class="star" value="5" />
        </div>

我试试这个

alert( $('#formComent .star1:checked').val() );

不起作用......

3 个答案:

答案 0 :(得分:3)

您需要在选择器中使用[name="star1"],因为它是名称,或.star因为它是类,而不是star1。您的formComment表单上可能包含ID,但由于它未包含在您的示例中,因此在选择器中也使用starVin

jQuery.noConflict();
alert( jQuery('#starVin input[name="star1"]:checked').val() );

alert( jQuery('#starVin .star:checked').val() );

http://jsfiddle.net/tayNH/

如果您要测试checked,可以使用is()方法:

$('#starVin input[name="star1"]').each(function(){ alert($(this).is(':checked')); });

答案 1 :(得分:0)

根据您的标记,我认为您需要$('#starVin input[name="star1"].star:checked').val()(将容器div的ID从'#formComent'更改为'#starVin',添加名称特异性'[name =“star1”]'并移动':选中'过滤到选择器,然后获取值。

See my jsfiddle for a working example

答案 2 :(得分:0)

如果有评级星,作为单选按钮,为了获得该评级星的价值,我已经编写了以下代码。它的工作对我来说很好。 这是我的html和jquery代码。

<fieldset class="rating">
    <input type="radio" class="get_value" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" class="get_value" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input type="radio" class="get_value" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" class="get_value"id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
    <input type="radio" class="get_value" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" class="get_value" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
    <input type="radio" class="get_value" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" class="get_value" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
    <input type="radio" class="get_value" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
    <input type="radio" class="get_value" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>

<script>
var star_rating = 0;
$(".get_value").click(function () {
    if($(this).prop("checked")) {
        star_rating = $(this).val();
        console.log(star_rating);
    }
});
</script>