简体   繁体   English

jQuery Star Rating选择值

[英]jQuery Star Rating Selected Value

I'm working the the jQuery Star Rating system that converts a drop down box into five stars, etc. It work okay but I just cannot get it to hold a preselected value when the page loads. 我正在使用jQuery Star Rating系统,它将一个下拉框转换为五颗星,依此类推。它可以正常工作,但是当页面加载时,我无法使它保持预先选择的值。 It always loads with nothing selected. 它始终在未选择任何内容的情况下加载。

Here is my select in test.html: 这是我在test.html中选择的内容:

<select name="rating" class="rating">
     <option value="1">Very poor</option>
     <option selected value="2">Not Bad</option>
     <option value="3">Average</option>
     <option value="4">Good</option>
     <option value="5">Perfect</option>
</select>

And my jQuery in test.html: 而我的jQuery在test.html中:

$(document).ready(function(){
$(".rating").rating();
});

And a section that should do what I need from jquery.rating.js 还有应该从jquery.rating.js执行我需要的部分

(function ($) {

$.fn.rating = function(options)
{

    var settings =
    {
       showCancel: true,
       cancelValue: null,
       cancelTitle: "Cancel",
       startValue: null,
       disabled: false
    };

Now according to the documentation, if startValue is NULL I should be able to use the selected="selected" option in my drop down, that didn't work so I set startValue equal to 2 and then when that didn't I tried "Not Bad" and still nothing. 现在,根据文档,如果startValue为NULL,则应该可以在下拉菜单中使用selected =“ selected”选项,但该选项无效,因此我将startValue设置为2,然后在没有尝试时将其设置为“还不错”,还是一无所获。 However, if I go and change disabled in the .js file to false, it locks out the star rating just as it should. 但是,如果我将.js文件中的disable更改为false,它将按原样锁定星级。 I'm stumped, anyone got any ideas? 我很困惑,有人有任何想法吗?

You could try like: 您可以尝试:

$('.rating > option[value=2]').attr('selected', true);

Above will set option with value 2 to be selected. 上面将设置值为2选项。 You need to adjust 2 above for whatever option you want to make selected in your drop down. 您需要为要在下拉菜单中选择的任何选项调整上述2

I know this is late to the game, but just in case you're still having this problem, or someone else runs into this question and needs an answer... this should work. 我知道这已经晚了,但是以防万一您仍然遇到这个问题,或者其他人遇到了这个问题并需要答案...这应该可行。

After this: 在这之后:

$(document).ready(function(){
    $(".rating").rating();
    ...

Try this: 尝试这个:

    ...
    $(".rating").val(2).change();

According to the docs, you have to specifically call ".change()" if you want to manually set the value. 根据文档,如果要手动设置该值,则必须专门调用“ .change()”。 I do this in one of my own sites. 我在自己的网站之一中进行此操作。 To be honest, I don't regard this plugin very highly, but sometimes you don't have a choice :-/ 老实说,我对这个插件的评价不高,但是有时候您别无选择:-/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM