在 jQuery HTML 计算商品及服务税

calculate GST in jQuery HTML

我正在尝试根据 input#input 计算 CGST/SGST/IGST。 我在这里考虑input#gst_rate = 18。 实际上,我正在尝试 - 如果用户选择 LOCAL,那么 input#gst_rate 将被分成 29 的值将显示在 input#cgst = 9input#sgst = 9input#igst = 0.

如果用户选择 INTERSTAE,则 input#gst_rate 将显示在 input#igst = 18input#cgst = 0 以及 input#sgst = 0 中。

我不熟悉 javascript 和 jQuery。请帮助我实现代码。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="gst_type" class="gst_type" id="gst_type">
  <option value="1">LOCAL</option>
  <option value="2">INTERSTATE</option>
</select>

<input type="text" class="form-control  gst_rate" id="gst_rate" value="18">
<br/><br/>
<input type="text" class="form-control  cgst" id="cgst" placeholder="CGST">
<input type="text" class="form-control  sgst" id="sgst" placeholder="SGST">
<input type="text" class="form-control  igst" id="igst" placeholder="IGST">

您可以研究这段显示您正在等待的结果的代码:

// initialize
var init_value = $("#gst_rate").val();
var choice = 2
$("#gst_type").val(choice).trigger("change");
interstate();
display(choice);


function interstate() {
  $("#cgst").val(0);
  $("#sgst").val(0);
  $("#igst").val(init_value);
}
function local() {
  let newvalue = init_value / 2;
  $("#cgst").val(newvalue);
  $("#sgst").val(newvalue);
  $("#igst").val(0);
}

function display(c){
  if(c == 1){
    local();
  }else if(c == 2){
    interstate();
  }
}
$("#gst_rate").on("change", function() {
  init_value = $("#gst_rate").val();
  display(choice);
});

$("#gst_type").on("change", function() {
  choice = $(this).val();
  display(choice);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="gst_type" class="gst_type" id="gst_type">
  <option value="1">LOCAL</option>
  <option value="2">INTERSTATE</option>
</select>

<input type="text" class="form-control  gst_rate" id="gst_rate" value="18">
<br/><br/>
<input type="text" class="form-control  cgst" id="cgst" placeholder="CGST">
<input type="text" class="form-control  sgst" id="sgst" placeholder="SGST">
<input type="text" class="form-control  igst" id="igst" placeholder="IGST">