在jQuery中拆分值 [英] Split values in jquery
本文介绍了在jQuery中拆分值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算税金,并且像这样在单个下拉列表中获取每个人的ID
I am trying to calculate tax also get the id for each in a single dropdown like this
<td><select name="tax[]" class="form-control tax" id="tax_1" style="width:80px;">
<option value="">Tax</option>
<?php $s1 = mysqli_query($con, "select * from taxes"); while($s2 = mysqli_fetch_array($s1)) {
$options .= "<option value='". $s2['tax_id']."_".$s2['name']."'>".$s2['name'].'-'.$s2['rate'] . "</option>";
?>
<option value="<?php echo $s2['tax_id']."_".$s2['rate']; ?>"><?php echo $s2['name'].'-'.$s2['rate']; ?></option>
<?php
}
?>
</select></td>
我将这些值传递给jquery以进行自动计算.值将类似于id_rate (1_5)
,我必须将其拆分和我应用于计算的费率.如何在那里拆分.
These values i am passing to jquery for auto calculation purpose. Values will be like id_rate (1_5)
, I have to split it and rate i should use for calculations. How to split it there.
现在我正在这样计算
$('body').on('change', '.quantity,.price,.discount,.tax', function() {
var tr = $(this).parent().parent();
var qty = tr.find('.quantity').val();
var price = tr.find('.price').val();
var tax = tr.find('.tax').val();
var dis = tr.find('.discount').val();
var amt = (qty * price) - (qty * price * dis) / 100;
var tax1 = (amt * (tax / 100));
tax1 = Number((tax1).toFixed(2));
但是我不知道如何拆分
推荐答案
您可以尝试使用split函数.
You can try split function.
$('body').on('change', '.quantity,.price,.discount,.tax', function() {
if($(this).hasClass("tax")){
var taxVal= $(this).val();
var tax_id=taxVal.split('_')[0];
var rate=taxVal.split('_')[1];
}
}
这篇关于在jQuery中拆分值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文