jQuery Validate插件不适用于规则('add')方法

时间:2013-02-05 14:05:47

标签: jquery jquery-validate

我有问题...... 我正在为sobipro开发一个joomla自定义搜索模块,它可以搜索特定类别,但不能搜索其子类别。 出于这个原因,我需要验证表单并将“sid_list1”选择列表类设置为“必需”。 我正在尝试使用JQuery Validate插件,但它对我不起作用。 这就是我如何设定他,任何想法?

<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>

3 个答案:

答案 0 :(得分:0)

您无法将rules('add')方法放在.validate()中。只有在特殊情况下才允许在.validate({rules:{...}})内放置一个函数...这不是其中之一。

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

这样做......

$(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/

答案 1 :(得分:0)

在我的情况下,$(this).rules('add', ...)通过在$(document).ready(...)内部调用来解决并正常工作。

答案 2 :(得分:0)

要让验证器按照假设的方式运行,您要验证的元素需要将属性名称设置为一个值(我猜是表单中唯一的值)。

如果我们查看您尝试验证的表单的消息和规则 您会注意到插件使用相关元素的名称作为属性的键。

我们可以通过以下行获取设置

$.data($("#my-form")[0], "validator").settings

如果您注意到消息或规则中的条目以“”(空字符串)作为关键字,那么您就知道您尝试验证的某些元素缺少 name 属性。

enter image description here

由于规则或消息的键为空,它几乎会应用于该表单内的每个输入,并且插件将显示奇怪的行为。