2014-11-17 81 views
1

我正在处理一个项目,我必须对输入字段的现有值进行一些计算。假设输入值是400或其他。使用.val()计算输入jQuery

下方我有一个选择框YES手段添加,NO手段添加或-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 */ 

我试图查找现有的例子,但我没有找到很多例子都非常感谢您的回答!

+0

你tottaly对的,我西港岛线编辑自己的帖子,谢谢! – Paul

+0

在“否”的情况下,您是否减去425,并且在哪个值中保留该值?这是一个重要的观点,否则你的问题不能得到妥善的回答。 – Christoph

+1

他的意思是,如果点击'yes',如果已经添加了'no',则应该减去425。 –

回答

6

的逻辑,这是一个比较复杂的。您需要知道在no已被点击之前是否已添加425,在这种情况下,您需要减去425,而不是仅添加0

考虑到这一点,你可以一个data属性添加到输入包含它的起拍价:

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

然后当select改变,你可以将数据属性转换为整数,然后进行计算在上面。试试这个:

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

+0

为什么你在val中使用函数(使用无用的参数)? – Christoph

+0

谢谢!这是我正在寻找的代码 – Paul

+0

@Christoph好点。我原本打算修改当前值,该函数对此有用。然而,由于已经引用了'priceTag'元素,所以不需要这些函数。 –

2

使用此:

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/

+0

如果您继续选择是/否/是/否,价格不会重置,而是每次增加425。 –

+0

感谢您解答这个问题。事实上,如果没有被选择,它必须减去 – Paul

+0

@Paul更新回答和JsFiddle –

0

保存的#priceTag网页加载数据的属性和值,每当用户改变它:

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>

0

这。

的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/

+0

如果您继续选择是/否/是/否,价格不会重置,而是每次增加425。 –

+0

@RoryMcCrossan然而,它改变了'input',所以我没有看到任何问题,但是你总是可以有一个隐藏的'input'来存储这个值,所以你永远不会碰到使用的实际'input'。 –

+0

@RoryMcCrossan固定为你想隐藏的输入。或者你也可以使用一个变量来存储答案 –