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>

对此的任何帮助都会很棒。

谢谢

...类似于 data = data + 1,但是我如何将值返回到文本框中?

4

4 回答 4

6

您可以使用 jQuery 的val()来获取和设置一个值。在这种情况下,您需要的代码可能如下所示(演示):

<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>
于 2013-03-22T17:50:04.417 回答
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
   })
});
于 2013-03-22T17:52:15.300 回答
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);
    }|

 });
于 2013-03-22T17:53:07.147 回答
1

您正在调用 Addone 函数内联,这意味着您的函数应该是 AddOne()

尝试这个

function AddOne(obj){
    var value=parseFloat(obj) + 1;
    $('#BoqTextBox').val(value);  
}
于 2013-03-22T17:50:50.823 回答