jquery - Dinamically renew calculator input result -
i have calculator based on select option, 2 inputs. dinamically result when change range or input data in text field. needed add div "buttons" fixed sizes. works great when choose select option , feel inputs. when choose select option click on div (60x90 or 100x150) button, inputs new values, result doesn't renewed. problem? think in $('#form').on('input change', 'select,input', function()
$(document).ready(function(){ $(document).on('input', '#height', function(event) { $(this).next().val($(this).val()); }); $(document).on('input', '#heightplus', function(event) { $(this).prev().val($(this).val()); }); $(document).on('input', '#width', function(event) { $(this).next().val($(this).val()); }); $(document).on('input', '#widthplus', function(event) { $(this).prev().val($(this).val()); }); }); $(document).ready(function(){ $('#form').on('input change', 'select,input', function() { console.log(this.type) if(this.type == 'range') $(this).next().text(this.value) var o = $(this).closest('.roword').find('select,input'), v = o.eq(0).val(), w = o.eq(1).val(), h = o.eq(3).val(), r = o.last().val(''); if(v) { v = v * w * h; r.val(v.tofixed()) } }) }); function changevalue($this) { if($($this).text() == "60x90") { $('input#heightplus, input#height').val('60'); $('input#widthplus, input#width').val('90'); } if($($this).text() == "100x150") { $('input#heightplus, input#height').val('100'); $('input#widthplus, input#width').val('150'); } }
.standart { border: 2px solid #000; display: inline-block; margin: 20px; padding: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="form"> <div class = "roword"> <div> <select name="type[]"> <option value="">choose type</option> <option value="1.1">type1</option> <option value="1.4">type2</option> </select> </div> <div class="form-col-2"> <input form="send" type="range" min="40" max="200" id="height" name="height[]" value="40"> <input form="send" type="text" maxlength="3" min="40" max="200" id="heightplus" name="heightplus[]" value="40" oninput="this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"> </div> <div class="form-col-3"> <input form="send" type="range" min="40" max="300" id="width" name="width[]" value="40"> <input form="send" type="text" maxlength="3" min="40" max="300" id="widthplus" name="widthplus[]" value="40" oninput="this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"> </div> <div class="col-md-2 col-sm-5 col-xs-10 form-col-4"> <input class="myprice" form="send" type="text" name="result[]" readonly> </div> </div> </div> <div class="col-md-2 col-sm-2 col-xs-5 post standart" onclick="changevalue(this)">60x90</div> <div class="col-md-2 col-sm-2 col-xs-5 post standart" onclick="changevalue(this)">100x150</div>
you can add $('#form input:first').trigger('change');
@ end of function, instead of adding trigger
in each if
, trigger , update values:
$(document).ready(function() { $(document).on('input', '#height', function(event) { $(this).next().val($(this).val()); }); $(document).on('input', '#heightplus', function(event) { $(this).prev().val($(this).val()); }); $(document).on('input', '#width', function(event) { $(this).next().val($(this).val()); }); $(document).on('input', '#widthplus', function(event) { $(this).prev().val($(this).val()); }); }); $(document).ready(function() { $('#form').on('input change', 'select,input', function() { console.log(this.type) if (this.type == 'range') $(this).next().text(this.value) var o = $(this).closest('.roword').find('select,input'), v = o.eq(0).val(), w = o.eq(1).val(), h = o.eq(3).val(), r = o.last().val(''); if (v) { v = v * w * h; r.val(v.tofixed()) } }) }); function changevalue(element) { if ($(element).text() == "60x90") { $('input#heightplus, input#height').val('60'); $('input#widthplus, input#width').val('90'); } if ($(element).text() == "100x150") { $('input#heightplus, input#height').val('100'); $('input#widthplus, input#width').val('150'); } $('#form input:first').trigger('change'); }
.standart { border: 2px solid #000; display: inline-block; margin: 20px; padding: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="form"> <div class="roword"> <div> <select name="type[]"> <option value="">choose type</option> <option value="1.1">type1</option> <option value="1.4">type2</option> </select> </div> <div class="form-col-2"> <input form="send" type="range" min="40" max="200" id="height" name="height[]" value="40"> <input form="send" type="text" maxlength="3" min="40" max="200" id="heightplus" name="heightplus[]" value="40" oninput="this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"> </div> <div class="form-col-3"> <input form="send" type="range" min="40" max="300" id="width" name="width[]" value="40"> <input form="send" type="text" maxlength="3" min="40" max="300" id="widthplus" name="widthplus[]" value="40" oninput="this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"> </div> <div class="col-md-2 col-sm-5 col-xs-10 form-col-4"> <input class="myprice" form="send" type="text" name="result[]" readonly> </div> </div> </div> <div class="col-md-2 col-sm-2 col-xs-5 post standart" onclick="changevalue(this)">60x90</div> <div class="col-md-2 col-sm-2 col-xs-5 post standart" onclick="changevalue(this)">100x150</div>
Comments
Post a Comment