简体   繁体   English

如何更改仅在html 5网页中显示的输入框的值

[英]how to change the value of input box just for display in html 5 web page

I have a textfield in which i am entering data i want that if user enter 1000 then it show 1,000 in textfield but this same value 1000 is also used in calculations further so how to solve this if user enter 1000 then just for display it show 1,000 and if we use in calcualtion then same var shows 1000 for calculating. 我有一个文本字段,我要在其中输入数据,我希望如果用户输入1000,那么它在文本字段中显示1,000,但是这个相同的值1000还会在计算中使用,因此,如果用户输入1000,然后仅显示它,该如何解决呢?显示1,000如果我们在计算中使用,则相同的变量将显示1000进行计算。

  <HTML>
  <body>
  <input type="text" id="test" value="" />
  </body>

   <script>


     var c=document.getElementById(test);
   </script>
  </html>

so if c user enter 1000 then it should dispaly 1,000 for dispaly one and if user uses in script 因此,如果c用户输入1000,则应该显示1,000,以显示一个,并且如果用户在脚本中使用

     var test=c

then test should show 1000 然后测试应该显示1000

document.getElementById returns either null or a reference to the unique element, in this case a input element. document.getElementById返回null或对唯一元素的引用,在本例中为input元素。 Input elements have an attribute value which contains their current value (as a string). 输入元素的属性value包含其当前值(作为字符串)。

So you can use 所以你可以使用

var test = parseInt(c.value, 10);

to get the current value . 得到当前值 This means that if you didn't use any predefined value test will be NaN . 这意味着,如果您不使用任何预定义值,则test将为NaN

However, this will be evaluated only once. 但是,这只会被评估一次。 In order to change the value you'll need to add an event listener, which handles changes to the input: 为了更改该值,您需要添加一个事件侦听器,该事件侦听器处理输入的更改:

// or c.onkeyup
c.onchange = function(e){
    /* ... */
}

Continuing form where Zeta left: Zeta离开的连续形式:

var testValue = parseInt(c.value);

Now let's compose the display as you want it: 1,000 现在,让我们根据需要组成显示:1,000

var textDecimal = c.value.substr(c.value.length-3); // last 3 characters returned
var textInteger = c.value.substr(0,c.value.length-3); // characters you want to appear to the right   of the coma
var textFinalDisplay = textInteger + ',' + textDecimal

alert(textFinalDisplay);

Now you have the display saved in textFinalDisplay as a string, and the actual value saved as an integer in c.value 现在,您已将显示内容保存为textFinalDisplay作为字符串,并将实际值保存为c.value中的整数

<input type="text" id="test" value=""></input>
<button type="button" id="get">Get value</input>

var test = document.getElementById("test"),
    button = document.getElementById("get");

function doCommas(evt) {
    var n = evt.target.value.replace(/,/g, "");
        d = n.indexOf('.'),
        e = '',
        r = /(\d+)(\d{3})/;

    if (d !== -1) {
        e = '.' + n.substring(d + 1, n.length);
        n = n.substring(0, d);
    }

    while (r.test(n)) {
        n = n.replace(r, '$1' + ',' + '$2');
    }

    evt.target.value = n + e;
}

function getValue() {
    alert("value: " + test.value.replace(/,/g, ""));
}

test.addEventListener("keyup", doCommas, false);
button.addEventListener("click", getValue, false);

on jsfiddle jsfiddle上

you can get the actual value from variable x 您可以从变量x获取实际值

<html>
<head>
    <script type="text/javascript"> 
        function abc(){
            var x = document.getElementById('txt').value;               
            var y = x/1000;
            var z = y+","+ x.toString().substring(1);
            document.getElementById('txt').value = z;
        }
    </script>
</head>
<body>
    <input type="text" id="txt" value="" onchange = "abc()"/>
</body>

This works with integer numbers on Firefox (Linux). 在Firefox(Linux)上可以使用整数。 You can access the "non-commaed"-value using the function "intNumValue()": 您可以使用函数“ intNumValue()”访问“非逗号”值:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
    String.prototype.displayIntNum = function()
    {
    var digits = String(Number(this.intNumValue())).split(""); // strip leading zeros
    var displayNum = new Array();
    for(var i=0; i<digits.length; i++) {
        if(i && !(i%3)) {
        displayNum.unshift(",");
        }
        displayNum.unshift(digits[digits.length-1-i]);
    }
    return displayNum.join("");
    }
    String.prototype.intNumValue = function() {
    return this.replace(/,/g,"");
    }
    function inputChanged() {
    var e = document.getElementById("numInp");
    if(!e.value.intNumValue().replace(/[0-9]/g,"").length) {
        e.value = e.value.displayIntNum();
    }
    return false;
    }
    function displayValue() {
    alert(document.getElementById("numInp").value.intNumValue());
    }
</script>
</head>
<body>
<button onclick="displayValue()">Display value</button>
<p>Input integer value:<input id="numInp" type="text" oninput="inputChanged()">
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不更改 select 框值的情况下选择选择框时更改 html 中的输入框值 - how to change input box value in html when selectbox is selected without changing the select box value 如何防止输入框中的数字只显示文本? - How to prevent numbers in the input box and just display text? 如何使用javascript通过另一个文本框的输入值更改两个文本框的显示值? - How to change the display value of two text boxes through the input value of another text box using javascript? 如何在附近的输入文本框中显示值 - How to display value in nearby input text box 如何使用php和html根据文本框中的输入值自动更改组合框值的值 - How to automatically change values of combobox value based on input value in text box using php and html 使用Vanilla javascript单击按钮后,如何将值设置为html的输入框并显示? - How can set value to input box of html and display after clicking a button using Vanilla javascript? 如何触发事件以更改输入框中的值? - How to Trigger event to change value in Input Box? 如何验证在输入框中输入的值只是数字? - How do I validate that value entered into an input box is just numbers? 更改@ html.DropDownList值时显示警报框 - Display the alert box when change the @html.DropDownList value 如何在 HTML 页面的多个位置实时显示更新的输入框值? - How to show updated input box value in multiple places on an HTML page in real time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM