0

我有一个用 jquery 检查的下拉菜单,如果选择了某个值,则会显示一个输入框。我需要将所选值与数组进行比较,如果选择与数组中的某个值匹配,则必须将该值添加到我的隐藏字段的标记中。非常感谢您的建议,因为我的 jquery 知识几乎没有

我当前的代码:(我将查询我的数据库以获取 Trekkers 的所有子值,这将是我的数组)

<script type="text/Javascript">
        jQuery(function ($){
            $(document).ready(function () {
                    $(".kw").hide();
                });

            $(function(){
                $('.postform').change(function() {
                    var selectData = $(this).attr("data-select");
                    var selectValue = $(this).val();
                    $('.kw').hide();
                     if($("fieldset[data-select='" + selectData + selectValue +"']").css("display") == "none"){
                         $("fieldset[data-select^='" + selectData + "']").hide();
                         $("fieldset[data-select='" + selectData + selectValue +"']").show();
                     }
                });
            });
        });
        </script>

<form role="search" method="get" id="equipfilter" action="">
 <fieldset>
  <select name="exc_equipment_cat" id="exc_equipment_cat" class="postform" data-select="select1">
    <option class="level-0" value="allerlei">Allerlei&nbsp;&nbsp;(10)</option>
    <option class="level-0" value="implemente">Implemente&nbsp;&nbsp;(97)</option>
    <option class="level-1" value="balers-toebehore">&nbsp;&nbsp;&nbsp;Balers &amp; Toebehore&nbsp;&nbsp;(4)</option>
    <option class="level-1" value="disse">&nbsp;&nbsp;&nbsp;Disse&nbsp;&nbsp;(4)</option>
    <option class="level-1" value="hammermeule">&nbsp;&nbsp;&nbsp;Hammermeule&nbsp;&nbsp;(4)</option>
    <option class="level-1" value="ploee">&nbsp;&nbsp;&nbsp;Ploeë&nbsp;&nbsp;(11)</option>
    <option class="level-1" value="ripper">&nbsp;&nbsp;&nbsp;Ripper&nbsp;&nbsp;(8)</option>
    <option class="level-0" value="trekkers">Trekkers&nbsp;&nbsp;(43)</option>
    <option class="level-1" value="case">&nbsp;&nbsp;&nbsp;Case&nbsp;&nbsp;(4)</option>
    <option class="level-1" value="fiat">&nbsp;&nbsp;&nbsp;Fiat&nbsp;&nbsp;(5)</option>
    <option class="level-1" value="ford">&nbsp;&nbsp;&nbsp;Ford&nbsp;&nbsp;(1)</option>
    <option class="level-1" value="john-deere">&nbsp;&nbsp;&nbsp;John Deere&nbsp;&nbsp;(13)</option>
    <option class="level-1" value="landini">&nbsp;&nbsp;&nbsp;Landini&nbsp;&nbsp;(5)</option>
    <option class="level-1" value="massey-ferguson">&nbsp;&nbsp;&nbsp;Massey Ferguson&nbsp;&nbsp;(3)</option>
    <option class="level-1" value="mccormick">&nbsp;&nbsp;&nbsp;McCormick&nbsp;&nbsp;(4)</option>
    <option class="level-1" value="new-holland">&nbsp;&nbsp;&nbsp;New Holland&nbsp;&nbsp;(8)</option>
 </select>
</fieldset>
<fieldset class="kw" data-select="select1 WHERE VALUE SHOULD BE ADDED" style="display: none;">
  <legend>Kw Range:</legend>
  <input type="text" name="kw_min" placeholder="min" value=""><br>
  <input type="text" name="kw_max" placeholder="max" value="">
 </fieldset>
 <fieldset>
  <legend>Price Range:</legend>
  <input type="text" name="pr_min" placeholder="from" value=""><br>
  <input type="text" name="pr_max" placeholder="to" value="">
 </fieldset>
<input type="submit" id="filtersubmit" value="Search">
</form>

这是对这个问题的回应

4

2 回答 2

1

我希望这可以帮助你

var values = ['a', 'b', 'c'];
jQuery('your-selector-to-select').change(function(){
    var j = jQuery(this);
    if(values.indexOf(j.val()) >= 0) {
        jQuery('hidden-selector').val(j.val());
    }
});

回答你的第一个问题:

jQuery('your-fieldset-selector').attr('data-select', 'select1 ' + j.val());
于 2012-11-26T08:40:06.667 回答
0

使用 inArray(value,array) 在数组中搜索并获得匹配的索引。

var yourarray = ['value1','value2'];
$('.matchedindex').text($.inArray($('.search').val(),yourarray) ;

如果$('.search').val()等于 value1则为$('.matchedindex').text()1

或者您可以使用 $.each 在数组中搜索:

var flag=false;
$.each(array,function(i,value){
    if(value.search($('.search').val())!=-1){
    # search is matched # you have index and value of array
        flag=true;
    }
});
于 2012-11-26T08:43:56.487 回答