简体   繁体   English

用输入jQuery的.val()计算

[英]Calculate with .val() of input jQuery

i'm working on a project, and i have to make some calculations on an existing value of an input field. 我正在做一个项目,并且我必须对输入字段的现有值进行一些计算。 let's say the input value is 400 or whatever. 假设输入值为400或其他值。

underneath i've got a select box YES means add 425 , NO means add 0 or substract -425; 在我下面有一个选择框, 表示添加425表示添加0或减去-425;

HTML: HTML:

<input id="priceTag" name="priceTag" type="text" value="400">

<select id="designChoice" name="designChoice">
     <option>Choose--</option>
     <option value="yes">yes</option>
     <option value="no">no</option>
</select>

jQuery: jQuery的:

jQuery(document).ready(function($){

    var priceTag = $('#priceTag');        

    $('#designChoice').on('change', function(){

        if($(this).val()==='yes'){              
          /* Here i want to add + 425 to the */

        }else{
          /* Here I want to add nothing to the input or substract -425 */   
        }

    });
});

What i've tried: 我试过的

priceTag.val(+ 425);
/* And more of this type of wrong code ;-P */

I've tried to look up existing examples but i didn't find many examples so thanks in advance for the answer! 我试图查找现有示例,但没有找到很多示例,因此在此先感谢您的回答!

The logic to this is a little more complicated. 逻辑上有些复杂。 You need to know if 425 has already been added before no has been clicked, in which case you need to subtract 425 , not just add 0 . 您需要知道是否已经添加425然后no单击no ,在这种情况下,您需要减去425 ,而不仅仅是加0

With that in mind you could add a data attribute to the input to contain it's starting price: 考虑到这一点,您可以将data属性添加到输入中以包含其起始价格:

<input id="priceTag" name="priceTag" type="text" value="400" data-default="400">

Then when the select is changed you can convert the data attribute to an integer and then perform the calculation on it. 然后,当select更改时,您可以将data属性转换为整数,然后对其进行计算。 Try this: 尝试这个:

jQuery(document).ready(function ($) {
    var $priceTag = $('#priceTag');        
    $('#designChoice').on('change', function () {
        if ($(this).val() === 'yes') {
            $priceTag.val(parseInt($priceTag.data('default'), 10) + 425);            
        } else {
            $priceTag.val($priceTag.data('default'));
        }        
    });
});

Example fiddle 小提琴的例子

Use this: 用这个:

JS: JS:

jQuery(document).ready(function($){

    var priceTag = $('#priceTag');        
    var selectedYes=false;
    $('#designChoice').on('change', function(){
        if($(this).val()==='yes'){              
          /* Here i want to add + 425 to the */
            selectedYes=true;
            priceTag.val( ( +priceTag.val() ) + 425 );
        }else if (selectedYes){
          /* Here I want to add nothing to the input */ 
            priceTag.val( ( +priceTag.val() ) - 425 );
            selectedYes=false;
        }

    });
});

JSFIDDLE: http://jsfiddle.net/ghorg12110/0xvkupe2/1/ JSFIDDLE: http : //jsfiddle.net/ghorg12110/0xvkupe2/1/

Save the value of #priceTag to a data attribute on page load and whenever the user changes it: 在页面加载时以及每当用户更改它时,将#priceTag的值保存到data属性中:

 jQuery(document).ready(function($){ var priceTag = $('#priceTag'), designChoice = $('#designChoice'); //save initial value and whenever it is changed to a data attribute priceTag.on('change', function() { $(this).data('value', this.value); designChoice.change(); }) .change(); designChoice.on('change', function(){ if($(this).val()==='yes') { /* Here i want to add + 425 to the */ priceTag.val( +priceTag.data('value') + 425 ); } else{ /* Here I want to add nothing to the input or substract -425 */ priceTag.val( priceTag.data('value') ); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="priceTag" name="priceTag" type="text" value="400"> <select id="designChoice" name="designChoice"> <option>Choose--</option> <option value="yes">yes</option> <option value="no">no</option> </select> 

Here it is. 这里是。

jQuery: jQuery的:

var priceTagAns = 0;   
$(document).ready(function(){   

    $('#designChoice').on('change', function(){

        if($(this).val()==='yes'){              
          /* Here i want to add + 425 to the */
           priceTagAns = (Number($("#priceTag").val()) + 425);
        }else{
          /* Here I want to add nothing to the input */ 
            priceTagAns = (Number($("#priceTag").val()));
        }
        alert(priceTagAns);
    });

});

The Alert is just to prove the value is set, do with it what you want. 警报只是为了证明已设置该值,并根据需要进行操作。

Working JSFiddle: http://jsfiddle.net/89z7qpkf/2/ 工作的JSFiddle: http//jsfiddle.net/89z7qpkf/2/

* UPDATED * * 更新 *

Also Variable version: http://jsfiddle.net/89z7qpkf/3/ 同样是可变版本: http : //jsfiddle.net/89z7qpkf/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM