2014-11-17 79 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固定爲你想隱藏的輸入。或者你也可以使用一個變量來存儲答案 –