1

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

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

3 回答 3

3

您不能将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/

于 2013-02-05T16:04:18.817 回答
0

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

于 2019-08-11T08:39:48.810 回答
0

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

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

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

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

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

在此处输入图像描述

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

于 2021-06-07T19:18:12.770 回答