【问题标题】:jscript 'type' attributejscript'类型'属性
【发布时间】:2012-02-26 21:30:43
【问题描述】:

我试图在 javascript 上实现以下逻辑。

如果类型是“bankAccountTypeId”,则使用 $(field.className) 获取与字段具有相同 className 的所有字段,然后使用 .each 循环遍历每个结果,将 field.value 与 $(this).val() 进行比较并使用 alert如果它们不同则显示错误消息(如果失败则中断)。

function onChange_productListField(field, type) {

  if (HimmsJSUtil.elementHasClass(field, 'DC')) {

        var allProductGroupFields = $(".DC."+type);

        var value = field.value;

        if (field.options) {

              value = HimmsJSUtil.getSelectedDropDownOption(field);

        }

        allProductGroupFields.each(function(index) {

              if ($(this).attr("id") != field.id

                          && !$(this).val()) {

                    $(this).val(value);

              }

        });

  } else {



        /* implement the logic here */

  }

}

我的问题是,在这个逻辑中 type 属性将如何工作?

【问题讨论】:

  • 抱歉,我对您的问题以及您的代码一无所知!你能解释你想要做什么吗?并发布 html 代码,这将使事情变得更快。

标签: javascript jquery


【解决方案1】:

首先让我们明确一点,jscript 不是 javascript,代码中的“类型”不是属性,而只是函数的一个参数。

var allProductGroupFields = $(".DC."+type);

上面的行使用 jQuery 选择一组同时具有“DC”和“bankAccountTypeId”类的元素,其中类型为“bankAccountTypeId”。这样的代码可以用在这样的结构中:

<div class="whatever">
    <input type="text" class="DC bankAccountTypeId" />
    <input type="text" class="DC userId" />
    <a href="http://stackoverflow.com/questions/1041344">
        jQuery multiple class selector
    <a>
</div>

额外:
对于这样的结构

<div class="DC">
    <input type="text" class="bankAccountTypeId" />
    <input type="text" class="userId" />
    <a href="http://stackoverflow.com/questions/3767512">
        jQuery class within class selector
    <a>
</div>

选择器行必须改为

var allProductGroupFields = $(".DC ."+type);

【讨论】:

  • 实际上,这将产生选择器".DC.bankAccountTypeId",它不会使用显示的结构进行选择。对于该结构,您需要生成选择器".DC .bankAccountTypeId"".DC &gt; .bankAccountTypeId"。或者将结构更改为&lt;div class="DC bankAccountTypeId"&gt;
  • This question 说明了如何在选择器中进行交集。没有交叉的空间。
  • 没错。这就是为什么您需要更改结构或选择器的原因。以jsfiddle.net/CmVPv 为例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2023-02-17
  • 2019-09-28
相关资源
最近更新 更多