【问题标题】:Fetch input type value by ID where display is not none通过 ID 获取输入类型值,其中 display 不是 none
【发布时间】:2013-01-27 15:35:40
【问题描述】:

我有多个与条件选择相关的字段。如果 Condition 与其他输入字段不匹配,则与上述 div ID 中的样式(display:none)相关联并且匹配保持活动状态,则在上述 div ID 中设置(display:block)。 这是在drupal中,所以我无法控制它...所以这里是生成的示例--

<div id="edit-field-p1brp-price" class="field-type-text field-name-field-p1brp-price field-widget-text-textfield form-wrapper" style="display: block;">
  <div id="field-p1brp-price-add-more-wrapper">
    <div class="form-item form-type-textfield form-item-field-p1brp-price-und-0-value">
      <label for="edit-field-p1brp-price-und-0-value">
        <input id="edit-field-p1brp-price-und-0-value" class="text-full form-text required" type="text" maxlength="255" size="60" value="5,98,000" name="field_p1brp_price[und][0][value]">
      </div>
    </div>
</div>
<div id="edit-field-sspps-price" class="field-type-text field-name-field-sspps-price field-widget-text-textfield form-wrapper" style="display: none;">
  <div id="field-sspps-price-add-more-wrapper">
    <div class="form-item form-type-textfield form-item-field-sspps-price-und-0-value">
      <label for="edit-field-sspps-price-und-0-value">
      <input id="edit-field-sspps-price-und-0-value" class="text-full form-text required" type="text" maxlength="255" size="60" value="4,65,000" name="field_sspps_price[und][0][value]">
      </div>
   </div>
</div>
<div id="edit-field-sspr1br-price" class="field-type-text field-name-field-sspr1br-price field-widget-text-textfield form-wrapper" style="display: none;">
  <div id="field-sspr1br-price-add-more-wrapper">
    <div class="form-item form-type-textfield form-item-field-sspr1br-price-und-0-value">
      <label for="edit-field-sspr1br-price-und-0-value">
      <input id="edit-field-sspr1br-price-und-0-value" class="text-full form-text required" type="text" maxlength="255" size="60" value="3,98,000" name="field_sspr1br_price[und][0][value]">
      </div>
    </div>
</div>

我必须只获取上面 DIV 中 DISPLAY 为 BLOCK 的那个输入的值。我尝试使用以下可见的 DIV ID,但它没有返回并尝试使用以上 DIV ID 和值也没有发生(这是我尝试的最后一个代码)--

if (($("#edit-field-p1brp-price-und-0-value:block").length > 0)){
  var price = $('#edit-field-p1brp-price-und-0-value').val();
}
if (($("#edit-field-sspps-price-und-0-value:visible").length > 0)){
  var price = $('#dit-field-sspps-price-und-0-value').val();
}
if (($("#edit-field-sspr1br-price-und-0-value:visible").length > 0)){
  var price = $('#edit-field-sspr1br-price-und-0-value').val();
}

更新---

我用于获取更改一个选择和警报的值的代码更新,但它只给出 NULL..

$(document).ready(function() {
        var price = null;
        $('div.form-item-field-membership-payment-type-und').change(function() {

          $("#edit-field-p1brp-price-und-0-value, #edit-field-sspps-price-und-0-value, #edit-field-sspr1br-price-und-0-value").each(function() {
                if($(this).is(':visible')) {
                    price = $(this).val();
                    alert(price);
                }
            });
        alert(price);   
    });  
    });

【问题讨论】:

  • 缩短这些 ID 将减少到客户端的总传输大小,从而加快页面速度。
  • 这是drupal渲染条件字段表单元素的方式...
  • 啊,那我相信你无能为力。但是,如果您查看您发布的 HTML,您会发现大约 80% 的 HTML 有效负载用于元素 ID。

标签: javascript jquery drupal


【解决方案1】:

你需要使用

$(selector).is(':visible')

看看这个 jsfiddle,遍历你的字段

http://jsfiddle.net/uuKHB/

更新

我为你更新了 jsfiddle:

http://jsfiddle.net/uuKHB/1/

$("#edit-field-p1brp-price-und-0-value, #edit-field-sspps-price-und-0-value, #edit-field-sspr1br-price-und-0-value").each(function() {
...
}

现在它会遍历您选择的输入(查看选择器,您可以使用逗号分隔添加多个元素

更新提醒

var price;
$("#edit-field-p1brp-price-und-0-value, #edit-field-sspps-price-und-0-value, #edit-field-sspr1br-price-und-0-value").each(function() {
    if($(this).is(':visible')) {
        price = $(this).val();
    }
});
alert(price);

像这样;)

这是带有警报的更新 jsfiddle

http://jsfiddle.net/uuKHB/2/

【讨论】:

  • 我在页面上也有其他输入类型..我不必获取它们的值
  • 如何提醒我正在经历的所有列出的 ID 的可见输入 ID 内的值? ?
  • 这对我有用,但我正在尝试 var price = $(this).val(); &如果我试图在外面提醒它......它没有给出价值,但里面给出了......我必须在外面使用价值......
  • 使价格在“全球”循环之外。然后将值添加到可变价格,然后提醒价格..应该可以解决问题。我更新了我的答案和 jsfiddle
  • 我试过这个..这里是 jFiddle....里面的 alert 给出了值,但外面只给出了 null 值...jsfiddle.net/5KvDV/2
【解决方案2】:

您没有设置 display 属性,display 的默认值是 inline-block。如果需要检查块或在条件中使用 inline-block,请将 display 属性设置为 block。您可以使用css() 访问显示属性。我想你有有效的 html 并且只有一个元素有 id = edit-field-p1brp-price-und-0-value

Live Demo

if($("#edit-field-p1brp-price-und-0-value").css('display') == 'block')
{
    var price = $('#edit-field-sspr1br-price-und-0-value').val();
}

如果 display 没有被阻塞,那么 else 部分会执行,这个可以检查 here.

【讨论】:

  • 如您所见,显示:none 是上述输入类型的 DIV 的属性...不起作用..
  • 您可以调试以检查出了什么问题,也可以为我们制作一个小提琴以找出问题所在。
猜你喜欢
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 2015-06-02
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多