Jquery val从属性html获取值而不是用户输入的值

时间:2016-12-21 06:48:37

标签: javascript jquery html

这是我的代码:

$(document).on('click','.addToCart',function(){
    a = $(this).data('code');
    b = $('#qty-'+a, this).val();
    // b = $('#input[name=qty-'+a+']').val();
    // b = $('#qty-quick-view', this).val();
    c = $('input[name=first_attribute]:checked').val();
    price = $(this).data('price');
    images = $(this).data('images');
    if($(this).hasClass('empty')){
        return false;
    }
    console.log(b);
    return false

    if($('.second-attribute').length > 0){
        d = $('input[name=second_attribute]:checked').val();
    }else{
        d = '';
    }

    e = {code:a,qty:b,first:c,second:d,'price':price,'images':images};

    if(b != ''){
        $.postdata($.root()+'order/add',e).done(function(data){

            if(data.status == 'out of stock'){
                alert('This product is out of stock');
            }else if (data.status == 'stock is not sufficient'){
                // alert('Sorry for inconvenient, ready stock '+data.qty);
                $('#qty-'+a).focus();
                $('#appendAlertStock').append($.texterror("Sorry only "+data.qty+" ready stock"));
                $('#qty-'+a).val(data.qty);
            }else{
                $('.total-cart').html(data.total);
                $('.mini-cart-autoload').load($.root()+'order/minicart');
                if($(window).width() > 1000){
                    $('.notif-cart,.head-header-right > li > ul').slideDown();
                }else{
                    $('.notif-cart,.small_cart').slideDown();
                }
                window.setTimeout(function() {
                    $(".notif-cart").slideUp(500, function(){
                        $(this).hide();
                    });
                }, 2000);
                $('#quick-view-modal').modal('hide');
                $("html, body").animate({ scrollTop: 0 }, 600);
            }
        });
    }else{
        alert ('Fill quantity column');
    }
    return false;
});

这是我的jquery代码,用于从id。

定义的指定输入中获取值

在这种情况下,例如我有2个页面使用相同的id,在第一页中用户输入的val返回号码但是当我到达第二个时,我只得到HTML值属性中的数字

以下是来自不同页面的HTML(但它们是相同的)

第一页:

<div class="col-md-6">
  <h5>{{trans('general.choose-quantity')}}: </h5>
  <input type="number" class="form-control availability-product" 
         value="1" id="qty-{{$product['id']}}" 
         name="qty-{{$product['id']}}" max="{{$firstchild[0]->stock}}">
</div>
<!-- col-md-6 -->

第二页:

<div class="col-md-6">
  <h5>{{trans('general.choose-quantity')}}: </h5>
  <input type="number" class="form-control availability-product" 
         value="1" id="qty-{{$product['id']}}" 
         name="qty-{{$product['id']}}" max="{{$firstchild[0]->stock}}">
</div>
<!-- col-md-6 -->

我做了但失败了

  1. 将id更改为单独的id,结果是两个输入都返回undefined
  2. 检查同一页面中的ID重复,但我认为情况并非如此
  3. 这个错误花了我一整天的时间来确定,有人知道如何解决这个问题吗?

    我尝试使用chrome和firefox,但输出相同。

0 个答案:

没有答案