验证jquery评级星

时间:2013-09-24 08:02:00

标签: javascript jquery ruby-on-rails-3 validation

嗨我试图用jquery验证验证一些jquery星。我在这里找到了几个答案,但两个都没有运气。

以下是视图源中的HTML导致im on rails

但它不起作用,我怎样才能验证评级明星,强制要求它们,XD!谢谢......我一直在调查这几天,请帮忙 -

正如我所说,我已经尝试过其他问题的答案,但它们并不适合我

<div id="pasescortos"> 
    <label for="attribute">Short passes</label>
<span class="star-rating-control">
<div class="rating-cancel"><a title="Cancel Rating"></a></div>
<div class="star-rating rater-0 star star-rating-applied star-rating-live" id="player_short_passes_1">

<a title="1">1</a></div><div class="star-rating rater-0 star star-rating-applied star-rating-live" id="player_short_passes_2">

<a title="2">2</a></div><div class="star-rating rater-0 star star-rating-applied star-rating-live" id="player_short_passes_3">

<a title="3">3</a></div><div class="star-rating rater-0 star star-rating-applied star-rating-live" id="player_short_passes_4">

<a title="4">4</a></div><div class="star-rating rater-0 star star-rating-applied star-rating-live" id="player_short_passes_5">

<a title="5">5</a></div></span>

<input class="star star-rating-applied" id="player_short_passes_1" name="player[short_passes]" type="radio" value="1" style="display: none;">

<input class="star star-rating-applied" id="player_short_passes_2" name="player[short_passes]" type="radio" value="2" style="display: none;">

<input class="star star-rating-applied" id="player_short_passes_3" name="player[short_passes]" type="radio" value="3" style="display: none;">

<input class="star star-rating-applied" id="player_short_passes_4" name="player[short_passes]" type="radio" value="4" style="display: none;">

<input class="star star-rating-applied" id="player_short_passes_5" name="player[short_passes]" type="radio" value="5" style="display: none;">

  </div>

这是JS。

$().ready(function() {

  // validate signup form on keyup and submit
$(".edit_player").validate({
ignore:'', // initialize the plugin
        errorElement: 'div'});




$("#player_position").rules("add", { required: true,  messages: { required: "<%= t('generales.camporequerido') %>"}});

$("#player_short_passes_1").rules("add", { required: true,  messages: { required: "<%= t('generales.camporequerido') %>"}});


$("#player_dominant_leg").rules("add", { required: true,  messages: { required: "<%= t('generales.camporequerido') %>"}});

});





jQuery.extend(jQuery.validator.messages, {
    required: "<%= t('generales.camporequerido') %>",
    remote: "Please fix this field.",
    email: "Ingresa un correo electrónico válido.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
    minlength: jQuery.validator.format("Please enter at least {0} characters."),
    rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
    range: jQuery.validator.format("Please enter a value between {0} and {1}."),
    max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
    min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});

EDIT1:

我在HTML查看源中注意到我的表单有novalidate =“”

 <form accept-charset="UTF-8" action="/player_steps/skills?locale=en" class="edit_player" id="edit_player_17" method="post" novalidate="novalidate">

奇怪的是我在Rails视图中没有novalidate标签,这是在jquery.validate.js中生成的吗?

这就是我在rails中的表单。

<%= form_for @player, :url => wizard_path do |f| %>

在关于doble id问题的jquery.rating.js中我发现了这个..我该如何解决?

// insert rating star
            var star = $('<div class="star-rating rater-'+ control.serial +'"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
            rater.append(star);

            // inherit attributes from input element
            if(this.id) star.attr('id', this.id);
            if(this.className) star.addClass(this.className);

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上,但是你有一些小错误。

首先,你有两件具有相同ID的东西 - 每个星星一个,每个收音机一个。每个元素只能有一个唯一的ID。在这种情况下调用$("#player_short_passes_1")时会发生什么变得未定义(很可能是你获得了第一个带有该ID的创建元素)。设置这些规则的更好方法是在验证调用中添加rules选项:

$(".edit_player").validate({
    ignore:'', // initialize the plugin
    errorElement: 'div',
    rules: {
      'player[short_passes]': {
        required:true
      }
   }
});

请记住,规则是由输入的名称定义的,而不是由ID定义的。请注意,必须在rules对象中引用player[short_passes]键,否则Javascript将引发错误。

另一件事是确保.edit_player实际上是一种形式。 jQuery Validate仅适用于表单,而不是div。

工作示例(ish - 我没有获得星标插件,因此您只需点击其中一个数字):http://jsfiddle.net/ryleyb/RghSK/