多维数组名称[*]选择器

时间:2018-07-16 14:39:29

标签: javascript jquery html

嗨,我正在尝试选择一个名称为Warehouse_row [*] [product]的表单输入,但是我的表单已设置为发布到多维数组中

这是我的表单架构和jquery代码。 我尝试获取数量字段的长度。

$(".erpPrdAdd").click(function(e) {
  e.preventDefault();
  console.log($.find($("input[name='warehouse_row[*][qty]']").length));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="erpWarehouseApp" class="erp_validate" novalidate="novalidate">

  <div class="areaProduct">
    <div class="areaProductRowInner">
      <div class="row ProductRow">
        <div class="col-sm-3">
          <input type="text" class="form-control" name="warehouse_row[0][product]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="" name="warehouse_row[0][info]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="1" name="warehouse_row[0][qty]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="0" name="warehouse_row[0][price]">
        </div>
      </div>
      <div class="row ProductRow">
        <div class="col-sm-3"><input type="text" class="form-control" name="warehouse_row[1][product]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="" name="warehouse_row[1][info]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="1" name="warehouse_row[1][qty]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="0" name="warehouse_row[1][price]"></div>
      </div>
    </div>

  </div>
  <div class="areaSum">
    <div class="row">
      <div class="col-sm-3"></div>
      <div class="col-sm-3"></div>
      <div class="col-sm-3"><span class="warehouseQtySum">0</span></div>
      <div class="col-sm-3"><span class="warehousePriceSum">0</span></div>
    </div>
  </div>
  <div class="areaBtn">
    <div class="row">
      <div class="col-sm-12 text-right">
        <button class="btn btn-primary erpPrdAdd" type="button">Εισαγωγή</button>
        <button class="btn btn-success" type="submit">Αποθήκευση</button>
      </div>
    </div>
  </div>



</form>

1 个答案:

答案 0 :(得分:0)

您可以使用包含*=或以$=结尾的选择器来定位所需的特定元素。

$(".erpPrdAdd").click(function(e) {
  e.preventDefault();

  console.log( $("input[name*='[qty]']").length );
  console.log( $("input[name$='[qty]']").length );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="erpWarehouseApp" class="erp_validate" novalidate="novalidate">

  <div class="areaProduct">
    <div class="areaProductRowInner">
      <div class="row ProductRow">
        <div class="col-sm-3">
          <input type="text" class="form-control" name="warehouse_row[0][product]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="" name="warehouse_row[0][info]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="1" name="warehouse_row[0][qty]">
        </div>
        <div class="col-sm-3">
          <input type="text" class="form-control" value="0" name="warehouse_row[0][price]">
        </div>
      </div>
      <div class="row ProductRow">
        <div class="col-sm-3"><input type="text" class="form-control" name="warehouse_row[1][product]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="" name="warehouse_row[1][info]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="1" name="warehouse_row[1][qty]"></div>
        <div class="col-sm-3"><input type="text" class="form-control" value="0" name="warehouse_row[1][price]"></div>
      </div>
    </div>

  </div>
  <div class="areaSum">
    <div class="row">
      <div class="col-sm-3"></div>
      <div class="col-sm-3"></div>
      <div class="col-sm-3"><span class="warehouseQtySum">0</span></div>
      <div class="col-sm-3"><span class="warehousePriceSum">0</span></div>
    </div>
  </div>
  <div class="areaBtn">
    <div class="row">
      <div class="col-sm-12 text-right">
        <button class="btn btn-primary erpPrdAdd" type="button">Εισαγωγή</button>
        <button class="btn btn-success" type="submit">Αποθήκευση</button>
      </div>
    </div>
  </div>



</form>