按ID获取输入类型值,其中display不是none

时间:2013-01-27 15:35:41

标签: javascript jquery drupal

我有多个与条件选择相关的字段。如果条件不匹配,则其他输入字段与上面的div ID&中的style(display:none)相关联。一个匹配保持活动状态,它在上面的div ID中设置(显示:块).. 这是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可见,但它没有返回&amp;尝试使用上面的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();
}

UPDATE ---

我正在使用的代码更新,用于获取一个选择和更改的值。警报,但它只给出了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);   
    });  
    });

2 个答案:

答案 0 :(得分: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 with alert

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

答案 1 :(得分:0)

您没有设置显示属性,显示的默认值将是内联块。如果需要检查块或在条件中使用内联块,请将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();
}

如果显示不是阻止,那么部分将执行,可以检查 here