来自输入属性 jQuery 的数组

array from input attribute jQuery

本文关键字:数组 jQuery 属性 输入      更新时间:2023-09-26

>需要检查每个输入是否具有属性inArray,然后显示此输入文件否则隐藏

我的网页代码

<label class="priceswrapper" tour_type="reg, div">Price
<input  name="adult_cost"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="reg, div">Price
<input  name="kids_cost"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="div">Price
<input  name="intro_dive"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="div">Price
<input  name="pro_dive"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="qud">Price
<input  name="quad_price"  type="text"/>
</lable>

j查询代码

jQuery(document).ready(function() {
  $('.priceswrapper').each(function(){
    var data = 'reg';
    var types = [$(this).attr('tour_type')];
    if ($.inArray(data, types) !== -1) {
        $(this).show();
            }else {
               $(this).hide();
               }
  });   
});

尝试如下:

<script>
jQuery(document).ready(function() {
      $('.priceswrapper').each(function(){
        var data = 'reg';
        var types = $(this).attr('tour_type').split(',');// you should get string fron here.
        console.log(types);
        if ($.inArray(data, types) !== -1) {
            $(this).show();
                }else {
                   $(this).hide();
                   }
      });   
    });

</script>
<label class="priceswrapper" tour_type="reg, div">Price
<input  name="adult_cost"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="reg, div">Price
<input  name="kids_cost"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="div">Price
<input  name="intro_dive"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="div">Price
<input  name="pro_dive"  type="text"/>
</lable>
<label class="priceswrapper" tour_type="qud">Price
<input  name="quad_price"  type="text"/>
</lable>
你可以

使用它。小提琴

.HTML

<label class="priceswrapper" data-type="reg, div">Price
    <input  name="adult_cost"  type="text"/>
</label>
<label class="priceswrapper" data-type="reg, div">Price
    <input  name="kids_cost"  type="text"/>
</label>
<label class="priceswrapper" data-type="div">Price
    <input  name="intro_dive"  type="text"/>
</label>
<label class="priceswrapper" data-type="div">Price
    <input  name="pro_dive"  type="text"/>
</label>
<label class="priceswrapper" data-type="qud">Price
    <input  name="quad_price"  type="text"/>
</label>

jQuery

    var data = 'reg';
    $('.priceswrapper').each(function(idx, obj) {
        var types = $(this).data('type').split(/,'s+/);
        console.log(types);
        console.log($.inArray(data, types));
        if ($.inArray(data, types) !== -1) {
            $(obj).show();
        } else {
            $(obj).hide();
        }
    });