2

I have created a number keypad using javascript. On each button click i have done the below:- Buttons are:-

<input type="button" value="2" class="num-key" onclick="insertvalue(this.value)">
<input type="button" value="3" class="num-key" onclick="insertvalue(this.value)">
<input type="button" value="4" class="num-key" onclick="insertvalue(this.value)">

the method is:-

function insertvalue(value)
{   
document.getElementById('number').value=document.getElementById('number').value + value;
}

It is working but it is to slow. If i will do this using jquery it might be fast. But i have done this using jquery

$( ".num-key" ).click(function() {
$(".num-text").val($(this).val());
});

But it is not working please tell me how to do this.

4

5 回答 5

2

尝试这个

$( ".num-key" ).on('click',function() {
      $(".num-text").val(($('.num-text').val()) + (this.value));
});

演示

于 2013-09-26T08:17:42.923 回答
2

将您的代码更改为

$(".num-key").click(function() {
    var inc = $(".num-text").val();      
    $(".num-text").val($(this).val() + inc);
});
于 2013-09-26T08:20:06.403 回答
1

如果要将其用作键盘,则应保留文本输入的先前值:

$(".num-key").click(function() {
    $(".num-text").val($(".num-text").val() + $(this).val());
});

这是链接: 演示

于 2013-09-26T08:25:34.137 回答
1

试试这个完整的键盘演示:http: //jsfiddle.net/devmgs/XGuVZ/

$( ".num-key" ).click(function() {
    if($(this).val()=="D")
         $(".num-text").val($(".num-text").val().substring(0, $(".num-text").val().length - 1)); 
    else if($(this).val()=="C")
         $(".num-text").val("");
    else
          $(".num-text").val($(".num-text").val()+$(this).val());
});
于 2013-09-26T08:33:32.637 回答
1

一个非常简单的方法是使用:(jsFiddle

$(".num-key").click(function () {
    var currentValue = $(".num-text").val();
    $(".num-text").val(currentValue + $(this).val());
});
于 2013-09-26T08:26:23.857 回答