I have two input elements and one select element. What I want is to get the values of the input elements and check the value of the select. the input elements are product base price and price-mark up so they are decimal numbers.
The select is with values 1 and 2. 1 is Fixed price and 2 is Percentage.
So, I want to check if the base price input field has a value and the price mark field has a value and check the select field.
If the input fields are not blank and the select = 1 ( fixed price ), then sum the base price and the price mark fields and set the result as a value of the 4th field, which is "price".
If the select value is = 2 (percentage) then get the base price and + or - the number in price mark ( considered as percentage).
So far I have the below code:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-base_price"><?php echo $entry_base_price; ?></label>
<div class="col-sm-10">
<input type="text" name="base_price" value="<?php echo $base_price; ?>" placeholder="<?php echo $entry_base_price; ?>" id="base_price" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-price_mark"><?php echo $entry_price_mark; ?></label>
<div class="col-sm-10">
<input type="text" name="price_mark" value="<?php echo $price_mark; ?>" placeholder="<?php echo $entry_price_mark; ?>" id="price_mark" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-price_mark_type"><?php echo $entry_price_mark_type; ?></label>
<div class="col-sm-10">
<select name="price_mark_type" id="price_mark_type" class="form-control">
<option value="1"><?php echo $entry_fixed_price;?></option>
<option value="2"><?php echo $entry_percent;?></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-price"><?php echo $entry_price; ?></label>
<div class="col-sm-10">
<script type="text/javascript">
base_price = $("#base_price").attr('value');
price_mark = $("#price_mark").attr('value');
price_mark_type = $("#price_mark_type").attr('value');
fixed = base_price + price_mark;
percent = base_price * (price_mark / 100);
if (base_price && price_mark == 1 ) {
$('#price').val(fixed);
}
if (base_price && price_mark == 2 ) {
$('#price').val(percent);
}
</script>
<input type="text" value="" name="price" placeholder="<?php echo $entry_price; ?>" id="price" class="form-control" />
</div>
</div>
I also tried and :
<script type="text/javascript">
var base_price = document.getElementById("base_price").value;
var price_mark = document.getElementById("price_mark").value;
var price_mark_type = document.getElementById("price_mark_type").value;
if (base_price && price_mark == 1 ) {
document.getElementById("price").value = 'parceInt(base_price) + parceInt(price_mark)';
}
if (base_price && price_mark == 2 ) {
document.getElementById("price").value = 'parceInt(base_price) * (parceInt(price_mark) / 100)';
}
</script>
I tried these suggestions but no result so far: How to set value of input text using jQuery
If you're using jQuery,
price_mark_type = $("#price_mark_type").val() should return 1 or 2.
base_price && price_mark == 1 should be instead base_price && price_mark_type == 1
var base_price = $("#base_price").val();
var price_mark = $("#price_mark").val();
var price_mark_type = $("#price_mark_type").val();
if (base_price && price_mark_type == 1) {
$("#price").val(parseInt(base_price) + parseInt(price_mark));
}
else if (base_price && price_mark_type == 2) {
$("#price").val(parseInt(base_price) + (parseInt(base_price) * (parseInt(price_mark) / 100)));
}
Worked with:
$('#price_mark').click(function(){
var base_price = $("#base_price").val();
var price_mark = $("#price_mark").val();
var price_mark_type = $("#price_mark_type").val();
if (base_price && price_mark_type == 1) {
$("#price").val(parseInt(base_price) + parseInt(price_mark));
}
else if (base_price && price_mark_type == 2) {
$("#price").val(parseInt(base_price) + (parseInt(base_price) * (parseInt(price_mark) / 100)));
}
});
Thanks to jeetaz!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.