JS onChange触发两次

时间:2017-03-12 13:50:14

标签: javascript jquery html

我有HTML和JS。但我不知道为什么当input = name [order_quantily]改变并运行2次因此它报告问题" Uncaught Error:语法错误,无法识别的表达式:tr [data-order-detail =]&#34 ;

我的HTML

<tr data-order-detail="9">
    <td>
        <select name="supplier[9]" class="form-control  "  id="9">  <option value="0" selected>
        ----------- </option>
            <option value="1" >
                skype:11112 </option>
        </select>                       </td>
    <td>
        <input type="text" name="order_quantily[9]" value="15" class="form-control readonlyinputNumber" id="9" placeHolder="">                      
    </td>
    <td>
        <input type="text" name="cost[9]" value="0" class="form-control readonly" id="cost[9]" placeHolder="">                      
    </td>
    <input type="hidden" name="currency_id[9]" value="1" class="form-control " id="" >
    <input type="hidden" name="supplier_product_id[9]" value="1" class="form-control " id="" ><input type="hidden" name="minQuantily[9]" value="0" class="form-control " id="" >
    <input type="hidden" name="maxQuantily[9]" value="0" class="form-control " id="" ><input type="hidden" name="price[9]" value="0" class="form-control " id="" >
</tr>

我的JS

$('[name^=supplier]').on('change', function() {
        var supplier_id = $(this).val();
            console.log(supplier_id);
        if (supplier_id != 0) {
            var id = $(this).prop('id');
            var jElement = $("tr[data-order-detail="+id+"]");
            var supplier_product_id = jElement.find('[name^=supplier_product_id]').val();
            var currency_id = jElement.find('[name^=currency_id]').val();
            var quantily = jElement.find('[name^=order_quantily]').val();

            $.ajax({
              method: "POST",
              url: "http://shop.local/admincp/admincp_order/admincp_voucher_observer/ajaxCal",
              data: { supplier_id: supplier_id, supplier_product_id: supplier_product_id, currency_id: currency_id, quantily: quantily }
            })
            .done(function( result ) {
                var result = JSON.parse(result);
                if (result.status == true) {
                    jElement.find('[name^=minQuantily]').val(result.minQuantily);
                    jElement.find('[name^=maxQuantily]').val(result.maxQuantily);
                    jElement.find('[name^=price]').val(result.price);

                    var newPrice = result.price*quantily;
                    jElement.find('[name^=cost]').val(newPrice);
                }
            });
        }
    });

    $('[name^=order_quantily]').on('change', function() {
        var id = $(this).prop('id');
        var jElement = $("tr[data-order-detail="+id+"]");
        var minQuantily =   parseInt(jElement.find('[name^=minQuantily]').val());
        var maxQuantily = parseInt(jElement.find('[name^=maxQuantily]').val());
        var quantily = parseInt(jElement.find('[name^=order_quantily]').val());

        if (minQuantily != 0 && maxQuantily != 0) {
            var price = parseFloat(jElement.find('[name^=price]').val());
            if ( minQuantily <= quantily && quantily <= maxQuantily) {
                var newPrice = quantily*price;
                jElement.find('[name^=cost]').val(newPrice);
            }
            else
            {
                var jSupplier = jElement.find('[name^=supplier]');
                jSupplier.trigger('change');
            }
        }
    });

0 个答案:

没有答案