简体   繁体   English

使用jQuery格式化textarea输入onload的值

[英]Formating the value of a textarea input onload with jQuery

Background: 背景:

I have a webpage with numerous textarea input fields that will load numeric values from a database each time the page is loaded (via commented tags the webserver looks for on each page load, and then inserts values in their place). 我有一个包含大量textarea输入字段的网页,每次页面加载时,该字段都会从数据库中加载数字值(通过注释标签,Web服务器在每次加载页面时都会查找该注释标记,然后在其位置插入值)。 For example: 例如:

<input type="text" value="<!--#etc etc etc -->"/>

The problem I'm having is the data being pulled from the database is formatted with extra decimal spaces I don't need to display, and I'm not able to reformatted the data in the database. 我遇到的问题是从数据库中提取的数据被格式化为不需要显示的多余小数位,而且我无法重新格式化数据库中的数据。

After some searching I did find a very helpful jquery tutorial (found here: how-to-limit-two-decimal-digits-in-a-jquery-input-field.html ) that does exactly what I need, but functions on the onkeyup event. 经过一番搜索后,我确实找到了一个非常有用的jquery教程(在这里找到: how-to-limit-two-decimal-digits-in-a-jquery-input-field.html ),它完全可以满足我的需要,但可以在onkeyup事件。 Since my values are inserted into the pages and not by a user, I need the function to run automatically as soon as the page finishes loading. 由于我的值是插入页面中的,而不是用户插入的,因此我需要该函数在页面加载完成后自动运行。 Is this possible? 这可能吗?

Below is my most recent attempt using the html/script from the tutorial as a base, but so far I haven't had any luck after revising the keyup event to (what I believe are) onload events: 以下是我最近一次尝试使用本教程中的html / script作为基础,但是到目前为止,将keyup事件修改为(我相信是)onload事件后,我还没有碰到什么:

<input type="text" name="one" class="two-digits" value="123.45126"><br>
<input type="text" name="two" class="two-digits" value="654.31221">

// apply the two-digits behaviour to elements with 'two-digits' as their class
$(window).load(function() {
$('.two-digits').ready(function(){
    if($(this).val().indexOf('.')!=-1){         
        if($(this).val().split(".")[1].length > 2){                
            if( isNaN( parseFloat( this.value ) ) ) return;
            this.value = parseFloat(this.value).toFixed(2);
        }  
     }            
     return this; //for chaining
});
});

Any input would be greatly appreciated! 任何投入将不胜感激!

EDIT: I forgot to mention the webserver is an embedded qnx slinger server. 编辑:我忘了提到Web服务器是嵌入式qnx甩干服务器。 So I don't have (and can't add) server side support for php, or any other common server side scripting languages. 因此,我没有(也不能添加)服务器端对php或任何其他常见服务器端脚本语言的支持。

If you are pulling the information from the database, and you are using PHP, you can use this function: 如果要从数据库中提取信息,并且正在使用PHP,则可以使用以下功能:

number_format($DB_VAL, 2);

that will round your number to be 2 numbers after the point. 将您的数字四舍五入为该点之后的2个数字。

EDIT: 编辑:

Use the .each jquery function to loop through your textboxes and alter their values with your displayed function. 使用.each jquery函数可循环浏览文本框,并使用显示的函数更改其值。

You need to use .each() . 您需要使用.each() This will call the function on every item with the class 这将在该类的每个项目上调用该函数

$('.two-digits').each(function(){
    if($(this).val().indexOf('.')!=-1){         
        if($(this).val().split(".")[1].length > 2){                
            if( isNaN( parseFloat( this.value ) ) ) return;
            this.value = parseFloat(this.value).toFixed(2);
        }  
     }            
     return this; //for chaining
});

http://jsfiddle.net/qFQCE/ http://jsfiddle.net/qFQCE/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM