使用jQuery单击按钮并显示按钮的值 [英] Using jQuery to click the button and display the value of the button

查看:144
本文介绍了使用jQuery单击按钮并显示按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,我正在尝试制作一个jquery ajax计算器.我已经为该接口创建了一个html.但是,我陷入了js中的点击功能部分.我想将显示器设置为默认值0,并且我想单击数字按钮并将其显示在显示器中.

hi i m new to jQuery and I am trying to make a jquery ajax calculator.I have created a html for the interface. However, I get stuck in the part of click function in js. I want to the display set 0 as default and i want to click the number button and display it in the display .

推荐答案

$(function(){
    // set value to 0 at first
    $('#display').val(0);

    $(document).on('click', 'button.number', function(){
        $('#display').val($(this).val());
    });
});

在这里测试: http://jsfiddle.net/xzg70up3/

保留数字:

$(function(){
    var $display = $('#display');
    $display.val(0);

    $(document).on('click', 'button.number', function(){
        if($display.val() != 0) {
            $display.val($display.val() + $(this).val());
        } else {
            $display.val($(this).val());
        }

    });
});

提琴: http://jsfiddle.net/qohdjovu/

这篇关于使用jQuery单击按钮并显示按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆