php - 如何在单击时更改文本字段的值?

标签 php javascript

我正在用 PHP 制作一个计算器。我一直遇到的一个问题是这个。

<input type="button" name="insert" value="0" style="width: 30px; height: 35px" onClick="zeroCheck()">

所以有一个仅包含“0”的文本字段,我希望它只用另一个零替换那个零,而不是在上面添加另一个“0”。

我的文本框名称叫做 entry。

为了解决这个问题,我尝试开发了这个功能,但没有成功。

function zeroCheck(){
    if (entry.value == '0'){
        entry.value=='0';
    } else{
        return entry.value+='0';
    }
}

这根本没有做任何事情。我在这里做错了什么?

如果有人能提供帮助,我们将不胜感激。

最佳答案

我能想到的最简单的解决方案是:

function zeroCheck(el) {
    // caching the value because it's used multiple times
    var v = el.value;
    /* setting the value with a ternary/conditional operator:
       v == '0' : testing that the value is equal to the given string
       if it is equal: the value is set to: '' + v (so there's no change),
       if it is not equal: the value is set to: '0' + v (so a zero is prepended).
    */
    el.value = ( v == '0' ? '' : '0' ) + v;
}

JS Fiddle demo .

在链接的演示中,上面的 JavaScript 与以下 HTML 一起使用:

<input type="button" name="insert" value="0" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="1" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="2" onClick="zeroCheck(this)" />
<input type="button" name="insert" value="3" onClick="zeroCheck(this)" />

添加一些(基本的)错误处理:

function zeroCheck(el) {
    // caching the value, because we're using it a few times
    var v = el.value,
    // saving the number (this way 1.4 and 1 are both preserved)
        num = parseFloat(v);

    // a basic check to see that the value is a number
    // (or rather that it is 'not Not-a-Number):
    if (!isNaN(num)) {
        el.value = ( v == '0' ? '' : '0' ) + v;
    }
    else {
        // default handling here, to handle non-numeric values:
        el.value = 0;
    }
}

JS Fiddle demo .

因为我认为我误读了这个问题,所以我用另一种可能性更新了这个答案,它处理同级文本输入元素并且还考虑了已经前导零的值:

function zeroCheck(el) {
    /* gets all the 'input' elements within the same parentNode that
       contains the button that's to be clicked: */
    var inputs = el.parentNode.getElementsByTagName('input'),
    /* declaring the variables to be used in the loop, so they're not
       being constantly re-declared through the loop */
        v, num;
    for (var i = 0, len = inputs.length; i < len; i++) {
        v = inputs[i].value;
        num = parseFloat(v);

        // if the input is the button, don't do anything, just keep going
        if (inputs[i] == el) {
            continue;
        // otherwise if the number is not Not-a-Number:
        } else if (!isNaN(num)) {
            inputs[i].value = (v.charAt(0) == '0' ? '' : '0') + v;
        } else {
            inputs[i].value = 0; // error-handling
        }
    }
}

JS Fiddle demo .

关于php - 如何在单击时更改文本字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16483651/

相关文章:

php - php中的trim()和strip_tags()可以防止sql注入(inject)吗?

php - 将链接中的文本值定位为一个输出中的多个链接

javascript - 如果在 css 中以百分比设置,我如何检索以像素为单位的 div 的最高值?

javascript - 将输入框中的值存储在数组中以计算总和

Javascript:未终止的字符串文字

javascript - 发送和处理表单数据

PHP7 - 不再支持/e 修饰符,请改用 preg_replace_callback

php - 使用jquery刷新div中的信息

php - 在一个查询中组合两个不同的过滤器 - Elastica

javascript - Google 脚本 - 用于多张工作表的一个运行编辑脚本