繁体   English   中英

用输入jQuery的.val()计算

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

我正在做一个项目,并且我必须对输入字段的现有值进行一些计算。 假设输入值为400或其他值。

在我下面有一个选择框, 表示添加425表示添加0或减去-425;

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(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 */   
        }

    });
});

我试过的

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

我试图查找现有示例,但没有找到很多示例,因此在此先感谢您的回答!

逻辑上有些复杂。 您需要知道是否已经添加425然后no单击no ,在这种情况下,您需要减去425 ,而不仅仅是加0

考虑到这一点,您可以将data属性添加到输入中以包含其起始价格:

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

然后,当select更改时,您可以将data属性转换为整数,然后对其进行计算。 尝试这个:

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'));
        }        
    });
});

小提琴的例子

用这个:

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/

在页面加载时以及每当用户更改它时,将#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> 

这里是。

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);
    });

});

警报只是为了证明已设置该值,并根据需要进行操作。

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

* 更新 *

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

暂无
暂无

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

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