简体   繁体   English

jQuery获取输入值

[英]JQuery get input value

HTML 的HTML

      <td data-title="Quantity">
         <div class="clearfix quantity r_corners d_inline_middle f_size_medium color_dark m_bottom_10">
           <button class="btn-minus bg_tr d_block f_left" data-item-price="8000.0" data-direction="down">-</button>
           <input min="0" class="f_left" type="text" value="2" name="order[line_items_attributes][0][quantity]" id="order_line_items_attributes_0_quantity">
           <button class="btn-plus bg_tr d_block f_left" data-item-price="8000.0" data-direction="up">+</button>
         </div>
         <div>
           <a href="#" class="color_dark remove-order"><i class="fa fa-times f_size_medium m_right_5"></i>Remove</a><br>
         </div>
     </td>

Javascript Java脚本

    $('.remove-order').click(function(){
       ????
    });

How to get the value of the input text when remove is click? 单击“移除”时如何获取输入文本的值?

You can use .val() 您可以使用.val()

$('.remove-order').click(function(){
   $('#order_line_items_attributes_0_quantity').val()
});
 $('.remove-order').click(function(){
     var input_val = $('#order_line_items_attributes_0_quantit').val();
     console.log(input_val);
 });
$('.remove-order').click(function () {d
    var myval="";
    myval = $('input.f_left').val();
    // or use:
    myval = $('input.f_left')[0].value;
});

In this case those would both return the 2 value as a string. 在这种情况下,它们都将以字符串形式返回2值。 IF you want a number be sure to parse it with parseInt(myval); 如果您想要一个数字,请务必使用parseInt(myval);对其进行解析parseInt(myval);

NOTE: since you have an ID you should use that selector: 注意:由于您具有ID,因此应使用该选择器:

var myvalInt = parseInt($('#order_line_items_attributes_0_quantity').val(), 10);

as that would be the fastest method. 因为那将是最快的方法。

Try closest(): 尝试close():

$('.remove-order').on('click', function(e){
  alert($(this).closest('td').find('input[name^=order]').eq(0).val());
});

Here, 这里,

  • .closest('td'), the nearest parent td . .closest('td'),最近的父级td
  • .find('input[name^=order]'), all input with name attribute starting with order .find('input [name ^ = order]'),所有具有name属性的输入均以order开头
  • .eq(0), first item in the list .eq(0),列表中的第一项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM