javascript - 使用 javascript 循环输入字段并隐藏

标签 javascript jquery html bigcommerce

我有一个页面,其中有一些动态创建的输入字段。我需要循环比较输入的值。如果值与针(搜索的值)匹配,则需要隐藏输入及其标签。以下是该页面的示例:

针:值=5762

<div id="AOSwatch" class="form-field" data-product-attribute="swatch">
    <label class="form-label form-label--alternate form-label--inlineSmall">
        Color:
        <span data-option-value></span>

            <small>Required</small>
    </label>

        <input class="form-radio AOformswatch" type="radio" name="attribute[1471]" value="5761" id="attribute_swatch_1471_5761"  required>
        <label class="form-option form-option-swatch" for="attribute_swatch_1471_5761" data-product-attribute-value="5761">
                        <span class='form-option-variant form-option-variant--color' title="Black" style="background-color: #252525"></span>
        </label>
        <input class="form-radio AOformswatch" type="radio" name="attribute[1471]" value="5762" id="attribute_swatch_1471_5762"  required>
        <label class="form-option form-option-swatch" for="attribute_swatch_1471_5762" data-product-attribute-value="5762">
                        <span class='form-option-variant form-option-variant--color' title="Brown" style="background-color: #5A442D"></span>
        </label>
        <input class="form-radio AOformswatch" type="radio" name="attribute[1471]" value="5763" id="attribute_swatch_1471_5763"  required>
        <label class="form-option form-option-swatch" for="attribute_swatch_1471_5763" data-product-attribute-value="5763">
                        <span class='form-option-variant form-option-variant--color' title="Navy" style="background-color: #1C3A6C"></span>
        </label>
</div>```

How would I do this using javascript? I already have some other javascript loading on the page, I think I'm just looking for a function or some code to loop through and do the hiding.

Thanks for any help or thoughts.

最佳答案

jQuery:

您迭代所有输入,如果找到一个值与目标值匹配的输入,则对其应用display: none。然后你还找到一个属性与值匹配的标签,并执行相同的操作。

var targetValue = 5762;
$("#AOSwatch").find("input").each(function(){
  if($(this).val() === targetValue){
      $(this).css("display", "none");
      $("#AOSwatch").find("label[data-product-attribute-value='"+targetValue+"']").css("display", "none");
  }
});

关于javascript - 使用 javascript 循环输入字段并隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60443126/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'src' of undefined at displayFull

javascript - javascript 中的 for..in 和 for each..in 有什么区别?

jquery - 向下滚动页面当前视口(viewport)的高度

html - ie7标准模式下的 float 问题

javascript - ASP.Net MVC : HTTP Error 414. 请求 URL 太长

html - 子元素覆盖父元素

javascript - svg #文档加载事件

javascript - 在 xp :table control 上启动数据表

javascript - 如何切换 div 的存在而不是可见性

javascript - 如何使用输入复选框检查特定图像