Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得类值_Javascript_Html - Fatal编程技术网

Javascript 如何获得类值

Javascript 如何获得类值,javascript,html,Javascript,Html,我想从输入字段中获取更新的值。所以我设置了一个监听器 $('.product-quantity input').change( function() { console.log($(this).parents('.product')[0].id) var id = $(this).parents('.product')[0].id; var classgroup = $(this).parents('.product')[0] for (var i = 0; i < classg

我想从输入字段中获取更新的值。所以我设置了一个监听器

$('.product-quantity input').change( function() {


console.log($(this).parents('.product')[0].id)
var id  = $(this).parents('.product')[0].id;
 var classgroup = $(this).parents('.product')[0]
 for (var i = 0; i < classgroup.childNodes.length; i++){

    if (classgroup.childNodes[i].className == "product-quantity") {

      console.log(classgroup.childNodes[i])
      var target = classgroup.childNodes[i]

    }
 }
 });

如何获取输出html的值编号?

这就是您要做的吗?您可以获取输入本身的
val
,并登录更改(或您想做的任何事情)

$('#productquantity')。更改(函数(){
log($(this.val());
});


FYI,您可以在较新的浏览器中将
$(this).parents('.product')[0]
替换为
this.parentNode.closest(.product”)
。如果
没有该类,则可以删除
.parentNode
。@rockstar是否与旧浏览器兼容?不,您需要一个多边形填充。不过,在我看来,这比加载一个大型库要好。
 <div class="product-quantity">
  <input type="number" id="productquantity" value="1" min="1">
 </div>
      target.each(function(){
        console.log(this.value)
      })