0

嗨,我正在尝试使用 jquery validate 验证一些 jquery 明星。我在这里找到了几个关于这个的答案,但都没有运气。

这是查看源代码中的 HTML,原因是我在轨道上

但它不起作用,我如何验证评级明星,使其成为强制性要求,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}.")
});

编辑1:

我在 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| %>

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

// 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);
4

1 回答 1

1

你在正确的轨道上,但你有几个小错误。

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

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

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

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

工作示例(ish - 我没有得到星号插件,所以您只需单击其中一个数字):http: //jsfiddle.net/ryleyb/RghSK/

于 2013-09-24T15:48:11.923 回答