单击查找文本字段值

时间:2017-10-16 11:31:52

标签: javascript jquery

当我点击加号或减号图标时,我必须找到qty的值。这是我的代码:

<div class='col-md-2 col-xs-2'>
  <img src='<?=base_url()?>assets/img/Plus.png' class='img-responsive carticons pull-right '>
</div>
<div class="col-md-4 col-xs-6">
  <div class="form-group" style="padding-top: 5px;">
    <input type="number" value="<?=$qty?>" class="qty pull-right" min="<?=$qty?> "name="qty">
  </div>
  <div class='col-md-2 col-xs-2' style='padding-left:0; '>
    <img src='<?=base_url()?>assets/img/Minus.png' class='img-responsive carticons pull-left'>
  </div>
  <input type='hidden' data-qty='<?=$qty;?>' class='minqty' value='<?=$value->qty;?>' />
$('.carticons').on('click',function(){
  var quantity = $(this).find('input[name=qty]').val();
  alert(quantity);
})

alert()中,它给了我undefined。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

使用$(this).parent().prev(".form-group").find('input[name=qty]').val()
$(this).closest(".col-md-4).find('input[name=qty]').val()

&#13;
&#13;
$('.carticons').on('click', function() {
  var quantity = $(this).parent().prev(".form-group").find('input[name=qty]').val();
  alert(quantity);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='col-md-2 col-xs-2'>
  <img src='<?=base_url()?>assets/img/Plus.png' class='img-responsive carticons pull-right '>
</div>
<div class="col-md-4 col-xs-6">
  <div class="form-group" style="padding-top: 5px;">
    <input type="number" value="<?=$qty?>" class="qty pull-right" min="" name="qty">

  </div>
  <div class='col-md-2 col-xs-2' style='padding-left:0; '>
    <img src='<?=base_url()?>assets/img/Minus.png' class='img-responsive carticons pull-left' alt="img">
  </div>
  <input type='hidden' data-qty='<?=$qty;?>' class='minqty' value='<?=$value->qty;?>' />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

假设您只有一个名为'qty'的输入,您只需要删除查找并直接引用该元素。

find()将搜索元素的后代并匹配提供的选择器,在您的情况下,单击的img没有后代。

$('.carticons').on('click',function(){
    var quantity = $('input[name=qty]').val();
    alert(quantity);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='col-md-2 col-xs-2'>
   <img src='http://placehold.it/50x50' class='img-responsive carticons pull-right '>
   </div>
    <div class="col-md-4 col-xs-6">
      <div class="form-group" style="padding-top: 5px;">
     <input type="number" value="<?=$qty?>" class="qty pull-right" min="<?=$qty?> "name="qty" >

</div>
<div class='col-md-2 col-xs-2' style='padding-left:0; '>
  <img src='<?=base_url()?>assets/img/Minus.png' class='img-responsive carticons pull-left'>
   </div>
   <input type='hidden' data-qty='<?=$qty;?>' class='minqty' value='<?=$value->qty;?>' />

答案 2 :(得分:0)

<div class='col-md-2 col-xs-2'>
  <img src='<?=base_url()?>assets/img/Plus.png' class='img-responsive carticons pull-right '>
</div>
<div class="col-md-4 col-xs-6">
  <div class="form-group" style="padding-top: 5px;">
    <input type="number" value="<?=$qty?>" class="qty pull-right" min="<?=$qty?> "name="qty">
  </div>
  <div class='col-md-2 col-xs-2' style='padding-left:0; '>
    <img src='<?=base_url()?>assets/img/Minus.png' class='img-responsive carticons pull-left'>
  </div>
  <input type='hidden' data-qty='<?=$qty;?>' class='minqty' value='<?=$value->qty;?>' />
</div>




<script>
  $(document).on('click','.carticons',function()
    {
      var quantity = $(document).find('input[name="qty"]').val();
      alert(quantity);
    })
</script>

您可以使用$(this)

代替$(document)