Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用oninput事件更改隐藏的输入价格_Javascript_Php_Jquery - Fatal编程技术网

Javascript 如何使用oninput事件更改隐藏的输入价格

Javascript 如何使用oninput事件更改隐藏的输入价格,javascript,php,jquery,Javascript,Php,Jquery,这是我的密码 这里是上一页的隐藏价格 <input disabled="disabled" type="text" class="field" style="text-align:center;" value="<?php if(isset($_POST['TotalPrice'])){$price =mysqli_real_escape_string($con,$_POST['TotalPrice']); echo $price; } ?>" /> <input

这是我的密码 这里是上一页的隐藏价格

<input disabled="disabled" type="text" class="field" style="text-align:center;" value="<?php if(isset($_POST['TotalPrice'])){$price =mysqli_real_escape_string($con,$_POST['TotalPrice']); echo $price; } ?>" />
<input type="text" size = "3" name="couponadd" id="couponadd" oninput="myFunction()" class="field" placeholder="Enter Coupon Code" />

必须使用jquery特定字段输入隐藏值:

<script>
    function myFunction() {
      var getcoupon = $("#couponadd").val(),
        txt='Invaild Coupon',
        price = $('input.field:disabled).val(), //Get value from hidden input
        discount = getcoupon == 'gold' ? .15 : getcoupon === 'silver' ? .1 : 0;

      price = price * (1 - discount); // calculate price after discount
    }
</script>

函数myFunction(){
var getculop=$(“#couponadd”).val(),
txt='Invaild优惠券',
price=$('input.field:disabled).val(),//从隐藏的输入中获取值
折扣=获得优惠券=='黄金'?.15:获得优惠券=='白银'?.1:0;
价格=价格*(1-折扣);//计算折扣后的价格
}

您的代码有多个错误

您不应将该字段设置为
已禁用
。它应该是
type=“hidden”
然后我附加了一个
id
,并得到了它的值。在检查折扣值之后,我再次将更新后的值指定给隐藏字段

<input type="hidden" id="total_price" class="field" style="text-align:center;" value="<?php if(isset($_POST['TotalPrice'])){$price =mysqli_real_escape_string($con,$_POST['TotalPrice']); echo $price; } ?>" />

<script>
 function myFunction() {
   var getcoupon = $("#couponadd").val();
   var total_price = $('#total_price').val();
      txt='Invaild Coupon';
   if (getcoupon == 'gold') {
     alert('Minus 15%');
     total_price = total_price - (total_price * 0.15);

   } else if (getcoupon == 'silver') {
     alert('Minus 10%');
     total_price = total_price - (total_price * 0.10);

   }
   $('#total_price').val(total_price);  // Here I am updating discounted value in the hidden field.
 }
</script>
试试捕鸟代码:

onload=函数(){
var e=document.getElementById('couponadd');
e、 oninput=函数(){
var getculop=$(“#couponadd”).val(),
txt='Invaild优惠券';
如果(Get优惠券=='gold'){
console.log(“负15%”);
}如果(Get优惠券=‘银’){
console.log('减10%);
}
e、 onpropertychange=e.oninput;//用于IE8
e、 onchange=e.oninput;
}
};


顺便说一句,您使用的是禁用字段,而不是隐藏字段。试着用一个合适的类或id来命名它,这样访问就更容易了。如果总价是100,你可以得到10%的折扣-你的逻辑将返回总价=10,但它应该是90次,谢谢你指出@NafiulIslam。我正在更新我的答案。请取消投票。@rahul,我想这会对你有帮助。你查过了吗?先生,从总价中减去15%或10%后,可能不会像120.5或156.6等那样。它应该是120或更高156@rahul,你能接受这个答案吗?因为它回答了你在主要问题中提出的问题?要使折扣价格成为一个整数值,您是想对该值进行四舍五入,还是只想删除小数部分。?
<input type="hidden" id="total_price" class="field" style="text-align:center;" value="<?php if(isset($_POST['TotalPrice'])){$price =mysqli_real_escape_string($con,$_POST['TotalPrice']); echo $price; } ?>" />

<script>
 function myFunction() {
   var getcoupon = $("#couponadd").val();
   var total_price = $('#total_price').val();
      txt='Invaild Coupon';
   if (getcoupon == 'gold') {
     alert('Minus 15%');
     total_price = total_price - (total_price * 0.15);

   } else if (getcoupon == 'silver') {
     alert('Minus 10%');
     total_price = total_price - (total_price * 0.10);

   }
   $('#total_price').val(total_price);  // Here I am updating discounted value in the hidden field.
 }
</script>