javascript - 来自输入属性 jQuery 的数组

标签 javascript jquery

<分区>

需要检查每个输入是否具有 inArray 属性然后显示此输入字段否则隐藏

我的html代码

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

jQuery 代码

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>

关于javascript - 来自输入属性 jQuery 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422655/

相关文章:

javascript - 限定 XHTML 中的 KnockOutJS 属性(通过 XML 命名空间)

javascript - 多个 webRTC 连接

javascript - 使用原型(prototype)观察多个选择菜单

jquery - 顶部导航随滚动消失

javascript - 单选按钮圆圈在选项卡中不可见

Titanium Mobile 的 JavaScript 库/框架

jquery - 使用 jQuery 和 Rails 3 实现 ajax 可排序列表

jquery - 使用 jQuery 更改 div 宽度

jquery datatables::对数据进行降序排序

javascript - 动态添加 ListView 时 jQuery Mobile 页脚未修复