2013-03-22 62 views
1

所以当页面加载时文本框中会包含一个存储值。我希望用户按下“+”按钮,文本框中的值将增加1。林猜测这是与jQuery做......在哪里开始任何想法,到目前为止,我...按下按钮并在文本框中增加值

<input type="text" name="BoqTextBox" id="BoqTextBox" value="0" /> 
    <input type="Button" value="+" onclick="AddOne(document.getElementById('BoqTextBox').value)" /> 

    <script> 
     function Add(data) { 
      //so the current digit is passed to here, where I need to do some funky code 
      //where it increments the current digit by one and stores it in BoqTextBox - replacing the old digit. 

      //Also to note if the text box contains 124.54 for example and + is pressed 
      //then new value will be 125.54 
     } 
    </script> 

任何这帮助将是巨大的。

谢谢

...像数据=数据+ 1,但后来我怎么返回值到文本框?

回答

6

您可以使用jQuery的val()来获取和设置一个值。在这种情况下,你需要看起来是这样的代码(demo):

<input type="text" name="BoqTextBox" id="BoqTextBox" value="0" /> 
<input type="Button" id='AddButton' value="+" /> 
<script> 
$('#AddButton').on('click', function() { 
    var input = $('#BoqTextBox'); 
    input.val(parseFloat(input.val()) + 1); 
}) 
</script> 
+0

函数调用是AddOne和你的函数是添加..你真的认为这将工作吗? – bipen 2013-03-22 17:57:16

+0

是的,当我试图建立一个JSFiddle时,我发现了这个问题。应该是固定的:) – 2013-03-22 17:57:53

+0

是的罚款现在... :)) – bipen 2013-03-22 17:59:12

1

你呼唤Addone内联函数这样就意味着你的函数应该是AddOne()

试试这个

function AddOne(obj){ 
    var value=parseFloat(obj) + 1; 
    $('#BoqTextBox').val(value); 
} 
+0

parseFloat会做.. :) ..试试我的更新 – bipen 2013-03-22 18:10:30

2
$('input[type="button"]').on('click', function() { // bind click event to button 
    $('#BoqTextBox').val(function() {  // change input value using callback 
     return ++parseFloat(this.value, 10); // make value integer and increment 1 
    }) 
}); 
+0

我喜欢'++ parseInt()'方法 – 2013-03-22 17:56:46

+0

@JasonSperske thx mate':)' – thecodeparadox 2013-03-22 17:57:01

1
$("#buttonId").click(function() 
{   
    var txtBox = $("#boqtextbox"); 

    if(!isNaN(txtBox.val())) 
    { 
     txtBox.val(parsFloat(txtBox.val())+1) ; 

    }else 
    { 
     //do validation or set it to 0 
     txtBox.val(0); 
    }| 

}); 
相关问题