jQuery Validate插件不适用于rules('add')方法 [英] jQuery Validate plugin does not work with rules('add') method

查看:111
本文介绍了jQuery Validate插件不适用于rules('add')方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题... 我正在为sobipro开发joomla自定义搜索模块,该模块可以在特定类别中进行搜索,但是无法在其子类别中进行搜索. 因此,我需要验证表单并将"sid_list1"选择列表类设置为必需". 我正在尝试使用JQuery Validate插件,但对我而言不起作用. 这是我设定他的方式,有什么想法吗?

I've a problem... I'm working on a joomla custom search module for sobipro, which can search in a specific category, but can't search in its sub-categories. For this reason i need to validate the form and set as "Required" the "sid_list1" select list class. I'm trying to use the JQuery Validate Plugin, but it doesen't work for me. This is how i set him, any idea?

<script text= type='text/javascript'> 
$("#XTspSearchForm132").validate(
{
    rules:
    {
        $('.sid_list1').each(function() {
    $(this).rules('add', {
        required: true,
        messages: {
            required:  "your custom message"
        }
    });
});
</script>

<form action="/index.php" method="post"
      id="XTspSearchForm132" >
    <div class="XTsearchhome">



<div class="XTSPSearch">
  <div></div>
  <div class="spspacer" style="clear:both;"></div>
  <div id="XTSPSearchForm">
    <div class="XTSPSearchCell1">
      <div class="XTSPSearchLabel">
        <strong>Search for: </strong>
      </div>
      <div class="XTSPSearchField"><input type="text" name="sp_search_for" size="20" value="visibile"  onblur="if (this.value=='') this.value='search...';" onfocus="if (this.value=='search...') this.value='';" class="XTSPSearchBox inputboxhome" id="XTSPSearchBox132" maxlength="20"/> </div>
    </div>
    <div class="XTSPSearchCell2">
      <div class="XTSPSearchField"><input type="submit" id="XTtop_button" name="search" value="Search" class="buttonhome" onclick="this.form.sp_search_for.focus();extSearchHelper132.extractFormValues();"/> </div>
    </div>

    <div class="XTSPSearchCell3">
      <div class="XTSPSearchLabel">
        <strong>Find entries that have: </strong>
      </div>
      <div class="XTSPSearchField"><span>
<!-- RadioButton 'spsearchphrase' Output -->
<input type="radio" name="spsearchphrase" id="XTspsearchphrase_all" value="all" checked="checked" />
<label for="XTspsearchphrase_all">All words</label>
<!-- RadioButton 'spsearchphrase' End -->
</span><span>
<!-- RadioButton 'spsearchphrase' Output -->
<input type="radio" name="spsearchphrase" id="XTspsearchphrase_any" value="any" />
<label for="XTspsearchphrase_any">Any words</label>
<!-- RadioButton 'spsearchphrase' End -->
</span><span>
<!-- RadioButton 'spsearchphrase' Output -->
<input type="radio" name="spsearchphrase" id="XTspsearchphrase_exact" value="exact" />
<label for="XTspsearchphrase_exact">Exact Phrase</label>
<!-- RadioButton 'spsearchphrase' End -->
</span> </div>
    </div>
    <div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
    <div class="spspacer" style="clear:both; min-height: 2px;"></div>
    <div id="XTSPExtSearch"><div class="XTSPSearchCell4"><div class="XTSPSearchLabel"><strong>Categories:</strong></div><div class="XTSPSearchField"><select class='sid_list1' name='to_sid_list_1321' id='extparent_1321' style='margin-top:4px;'><option style="" value="" selected="selected">- Seleziona categoria -</option><option style="" value="spc55" class="spc63" >Africa del Sud</option><option style="" value="spc56" class="spc63" >Nord America e Caraibi</option><option style="" value="spc57" class="spc63" >America Centrale</option><option style="" value="spc58" class="spc63" >Sud America</option><option style="" value="spc59" class="spc63" >Isole del Pacifico</option><option style="" value="spc60" class="spc63" >Oceano Indiano</option><option style="" value="spc61" class="spc63" >Penisola Arabica</option></select><select class='sid_list2' name='to_sid_list_1322' id='extparent_1322' style='margin-top:4px;'><option style="" value="" selected="selected">- Seleziona categoria -</option><option style="" value="spc62" class="spc55" >Botswana</option><option style="" value="spc64" class="spc55" >Mauritius e Seychelles</option><option style="" value="spc65" class="spc55" >Mozambico</option><option style="" value="spc66" class="spc55" >Namibia</option><option style="" value="spc67" class="spc55" >Zimbabwe e Zambia</option><option style="" value="spc68" class="spc55" >Sudafrica</option><option style="" value="spc69" class="spc56" >Anguilla</option><option style="" value="spc70" class="spc56" >Antigua e Barbados</option><option style="" value="spc71" class="spc56" >Aruba</option><option style="" value="spc72" class="spc56" >Bahamas</option><option style="" value="spc73" class="spc56" >Bermuda</option><option style="" value="spc74" class="spc56" >Giamaica</option><option style="" value="spc75" class="spc56" >Rep Dominicana</option><option style="" value="spc76" class="spc56" >St Lucia</option><option style="" value="spc77" class="spc56" >St Martin</option><option style="" value="spc78" class="spc56" >Turks and Caicos</option><option style="" value="spc79" class="spc56" >US &amp; British Virgin Islands</option><option style="" value="spc80" class="spc56" >Canada</option><option style="" value="spc81" class="spc56" >Canouan Island</option><option style="" value="spc82" class="spc56" >Stati Uniti</option><option style="" value="spc83" class="spc57" >Belize</option><option style="" value="spc84" class="spc57" >Costa Rica</option><option style="" value="spc85" class="spc57" >Guatemala</option><option style="" value="spc86" class="spc57" >Messico</option><option style="" value="spc87" class="spc57" >Panama</option><option style="" value="spc88" class="spc58" >Equador</option><option style="" value="spc89" class="spc58" >Perù</option><option style="" value="spc90" class="spc59" >Hawaii</option><option style="" value="spc91" class="spc59" >Polinesia Francese</option><option style="" value="spc92" class="spc60" >Seishelles</option><option style="" value="spc93" class="spc60" >Mauritius</option><option style="" value="spc94" class="spc61" >Dubai</option><option style="" value="spc95" class="spc61" >Oman</option></select></div></div><div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
      <div class="XTSPSearchCell5">
        <div class="XTSPSearchLabel">
          <strong>Prodotto: </strong>
        </div>
        <div class="XTSPSearchField"><select id="XTfield_prodotto" name="field_prodotto" id="XTfield_prodotto" class="XTSPSearchSelect" >
    <option value=""> --- Select Prodotto  --- </option>
    <option value="prodotto_option_12">Noleggio auto</option>
    <option value="prodotto_option_11">Escursioni</option>
    <option value="prodotto_option_10">Soggiorni Mare</option>
    <option value="prodotto_option_9">Tour in Auto</option>
    <option value="prodotto_option_8">Tour di Gruppo</option>
    <option value="prodotto_option_7">Minitour</option>
    <option value="prodotto_option_6">Noleggio moto</option>
    <option value="prodotto_option_5">Noleggio camper</option>
</select> </div>
      </div>
      <div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
      <div class="XTSPSearchCell6">
        <div class="XTSPSearchLabel">
          <strong>Paese: </strong>
        </div>
        <div class="XTSPSearchField"><select id="XTfield_paese" name="field_paese" id="XTfield_paese" class="XTSPSearchSelect" >
    <option value=""> --- Select Paese  --- </option>
    <optgroup label="Africa del Sud">
        <option value="paese_option_7">Botswana</option>
        <option value="paese_option_10">Mauritius e Seychelles</option>
        <option value="paese_option_9">Mozambico</option>
        <option value="paese_option_11">Nambia</option>
        <option value="paese_option_12">Sudafrica</option>
        <option value="paese_option_8">Zimbawe e Zambia</option>
    </optgroup>
    <optgroup label="America Centrale">
        <option value="paese_option_18">Belize</option>
        <option value="paese_option_17">Costarica</option>
        <option value="paese_option_16">Guatemala</option>
        <option value="paese_option_15">Messico</option>
        <option value="paese_option_14">Panama</option>
    </optgroup>
    <optgroup label="Nord America e Caraibi">
        <option value="paese_option_37">Anguilla</option>
        <option value="paese_option_36">Antigua e Barbados</option>
        <option value="paese_option_35">Aruba</option>
        <option value="paese_option_34">Bahamas</option>
        <option value="paese_option_33">Bermuda</option>
        <option value="paese_option_26">Canada</option>
        <option value="paese_option_25">Canouan Island</option>
        <option value="paese_option_32">Giamaica</option>
        <option value="paese_option_31">Rep Dominicana</option>
        <option value="paese_option_30">St Lucia</option>
        <option value="paese_option_29">St Martin</option>
        <option value="paese_option_24">Stati Uniti</option>
        <option value="paese_option_28">Turks and Caicos</option>
        <option value="paese_option_27">US &amp; British Virgin Islands</option>
    </optgroup>
    <optgroup label="Oceano Indiano">
        <option value="paese_option_178">Seishelles</option>
        <option value="paese_option_177">Mauritius</option>
    </optgroup>
    <optgroup label="Pacific Islands">
        <option value="paese_option_149">Hawaii</option>
        <option value="paese_option_148">Polinesia Francese</option>
    </optgroup>
    <optgroup label="Penisola Arabica">
        <option value="paese_option_182">Dubai</option>
        <option value="paese_option_181">Oman</option>
    </optgroup>
    <optgroup label="Sud America">
        <option value="paese_option_146">Equador</option>
        <option value="paese_option_145">Perù</option>
    </optgroup>
</select> </div>
      </div>
      <div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
      <div class="XTSPSearchCell7">
        <div class="XTSPSearchLabel">
          <strong>Stile di Viaggio: </strong>
        </div>
        <div class="XTSPSearchField"><select id="XTfield_temi" name="field_temi" id="XTfield_temi" class="temi SPSearchSelect">
    <option value=""> --- Select Stile di Viaggio  --- </option>
    <option value="stile_option_13">Luna di Miele</option>
    <option value="stile_option_12">Avventure</option>
    <option value="stile_option_11">Natura e Animali</option>
    <option value="stile_option_10">Deserto</option>
    <option value="stile_option_9">Cultura e Antiche Civiltà</option>
    <option value="stile_option_8">Spiaggia Mare e Relax</option>
    <option value="stile_option_7">Sott'Acqua</option>
    <option value="stile_option_6">On the Road</option>
    <option value="stile_option_5">Shopping nelle Grandi Città</option>
    <option value="stile_option_4">Mai più Soli</option>
    <option value="stile_option_3">Spa e Benessere</option>
    <option value="stile_option_2">Famiglie</option>
</select> </div>
      </div>
      <div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
      <div class="XTSPSearchCell8">
        <div class="XTSPSearchLabel">
          <strong>Cerca nelle offerte: </strong>
        </div>
        <div class="XTSPSearchField"><div id="XTfield_offerte">
<div ><span>
<!-- CheckBox 'field_offerte[]' Output -->
<input type="checkbox" name="field_offerte[]" id="XTfield_offerte_field_offerte_option_1" value="field_offerte_option_1" class="XTSPSearchChbx" />
<label for="XTfield_offerte_field_offerte_option_1">Sì</label>
<!-- CheckBox 'field_offerte[]' End -->
</span></div></div><div class="spspacer" style="clear:both;"></div> </div>
      </div>
      <div class="spspacer" style="clear:both; margin-bottom: 10px;"></div>
    </div>
  </div>
  <div class="spspacer" style="clear:both;"></div>
  <div class="XTSPListing">
    <div class="XTSPEntriesListContainer"></div>
    <div class="spspacer" style="clear:both;"></div>
    <div class="spspacer" style="clear:both;"></div>
  </div>
</div>
<div>

<input type="hidden" id="XTSP_sid" name="sid" value="54"/>
<input type="hidden" id="XTSP_task" name="task" value="search.search"/>
<input type="hidden" id="XTSP_option" name="option" value="com_sobipro"/>

            <input type="hidden" id="sid_list132" name="sid_list" value=""/>
                    <input type="hidden" id="XTSP_Itemid" name="Itemid" value="101"/>
    </div></div>
</form>

推荐答案

您不能放置 .validate() .仅在特殊情况下,才允许在.validate({rules:{...}})中放置一个函数...这不是其中之一.

You cannot put the rules('add') method inside of .validate(). Only in special situations are you allowed to put a function inside of .validate({rules:{...}}) ... this is not one of them.

rules('add')是一个完全独立的方法,因此,它独立于.validate()使用.这就是拥有单独方法的全部要点.您可以使用.validate()初始化表单,然后可以使用其他方法进一步操作该插件.

rules('add') is a whole separate method, and as such, gets used independently from .validate(). That's the whole point of having a separate method. You use .validate() to initialize the form, and then you can use other methods to manipulate the plugin further.

像这样...

$(document).ready(function () {

    // initialize the plugin with .validate()
    $("#XTspSearchForm132").validate({
        // other rules and options
    });

    // use rules() method to add rules by class name
    $('.sid_list1').each(function () {
        $(this).rules('add', {
            required: true,
            messages: {
                required: "your custom message"
            }
        });
    });

});

工作示例: http://jsfiddle.net/RtFKc/

Working Demo: http://jsfiddle.net/RtFKc/

这篇关于jQuery Validate插件不适用于rules('add')方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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