如何在JavaScript代码中发送2个参数

时间:2018-12-26 16:29:39

标签: javascript

我想用一个参数调用一个keyup函数。而且我还想获​​取关键代码。怎么做我的代码在下面。

                //this code part in ajax function


                '<td style="padding-left:10px;">'+
                '<input type="text" name="unitDiscount[]" id="unitDiscount'+count+'" onkeyup="getDiscountTotal('+count+')" autocomplete="off" class="form-control" />';
        if(tableLength > 0) { 
          $("#productTable tbody tr:last").after(tr);
         } else {
            $("#productTable tbody").append(tr);
         }
    } //end success
  });// ajax end



function getDiscountTotal(row = null) {
     if(row) {
       var tot =  Number($("#quantity"+row).val()) * Number($("#unitDiscount"+row).val());
       $("#price"+row).val(tot);
       subAmount();

       function myEvent(event) {          
         var x = event.keyCode;
          if (x == 32) {         
            $( "#paid" ).focus();
           }
         }

     } else {
       alert('no row !! please refresh the page');
     }
   }//end getDiscountTotal

我想知道如何将事件参数与其他参数一起发送。请帮助我。

1 个答案:

答案 0 :(得分:0)

在这里看看,我们使用addEventListener触发了多个功能。

reader
// Add Event to all inputs.
Array.from(document.getElementsByClassName('form-control')).forEach(discountInput => {
  console.log(discountInput);
  // Check it is discount input
  if (discountInput.dataset.unitDiscountId) {
    // Discount Total
    discountInput.addEventListener('keyup', function(event) {
      console.log(event.target.dataset.unitDiscountId);
      getDiscountTotal(event.target.dataset.unitDiscountId);
    });
    // Key 32 Check
    discountInput.addEventListener('keyup', function(event) {
      var x = event.keyCode;
      console.log(x);
      if (x == 32) {
        $("#paid").focus();
      }
    });
  }
});

function getDiscountTotal(row = null) {
  if (row) {
    console.log(`Called with row ${row}`);
    // Commented out as I dont have access to this code.
    /*
    var tot =  Number($("#quantity"+row).val()) * Number($("#unitDiscount"+row).val());
    $("#price"+row).val(tot);
    subAmount();
    */
  } else {
    alert('no row !! please refresh the page');
  }
} //end getDiscountTotal