2012-05-27 68 views
-2

我正在为uni主题进行分配,并且在使用javascript时遇到了一些麻烦。我希望能够根据另一个字段的值更改输入字段的值。简而言之,其目的是在一个字段中输入一定数量的产品,并且将其旁边的字段更改为显示购买该数量产品所需的总金额。使用javascript更改输入值

当我运行我的HTML时,输入的数量不会改变成本字段的值,我认为我的JavaScript一定有什么问题。

这里是我的javascript函数的副本,以及它在其内部执行的相关html。

SCRIPT:

function calcRowWash(){ 
    var theForm = document.forms["orderform"]; 
    var x = theForm.getElementById("quantc").value; 
    var quantity = 0; 
    if(x.value!=""){ 
     quantity = parseInt(x.value); 
    } 
    var totalC = (quantity*0.30); 
    document.getElementById("totc").value = totalC; 
    return; 
} 

HTML:

<td width = "90px" align ="left"><input type = "text" id ="quantc" name = "quantWash" size = "5" tabindex = "13" onblur="calcRowWash()"/></td> 
<td width = "90px" align ="left"><input type = "hidden" id ="totc" name = "washtotal" size = "5" tabindex = "14" value=""/></td>  

感谢您的帮助!

+0

我试图格式化你的代码(作为代码)以帮助,并删除不必要的帮助提示符('''输入代码在这里''消息)。我不是想要竞争。 –

+0

尝试调试您的代码并查看发生了什么。 –

回答

0
var theForm = document.forms["orderform"]; 
var x = theForm.getElementById("quantc").value; 

这是多余的。 ID是对整个文档唯一的,所以这个就足够了:

var x = document.getElementById('quantc'); 

另外请注意,我删除了.value - 这是问题,因为你再试图获得的价值...价值。

+1

请问downvoter,请解释为什么答案不令人满意? –

-2

您可以使用此JavaScript代码:

var inp = document.getElementById("inp"); // getting the Input ID 
function change_value() { 
    inp.value = 'Your Value'; // <-- value 
} 

您可以添加活动:

inp.onfocus = change_value; 
inp.onblur = another function with another action; 

好运利亚姆和拥有美好的一天。

+0

不以任何方式回答问题... –

-1
<form name="myForm" action="#" method="POST"> 
    <input type = "text" id="quantc" name = "quantWash" size = "5" tabindex = "13" onblur="calcRowWash()"/> 
    <input type = "hidden" id ="totc" name = "washtotal" size = "5" tabindex = "14" value=""/> 
</form> 

function calcRowWash(){ 
    var quantity = document.myForm.quantWash.value; 
    var price = 10.0; 
    document.myForm.washtotal.value = quantity * price; 
} 

该函数不包含解析的东西。我只是想展示如何读取和设置两个输入字段的值。

+0

这不起作用,我明白如何阅读输入字段。但由于某种原因,运行包含脚本的页面并在输入字段中输入值时,washtotal字段的值不会显示任何内容,并且保持空白。为什么这是我的问题...不应该由onblur事件引发的功能? – Bundy

+0

您的第二个输入字段是隐藏的 nullpointr

+0

你是对的。我不得不使用相同的表格和JavaScript创建一个新页面,以便它能够正常工作。我的javascript或其他地方的html一定有问题 – Bundy

0

This Works。

calcRowWash = (function(){ 
    var x = document.getElementById("quantc"); 
    var quantity = 0; 

    if (x.value!="") quantity = parseInt(x.value); 

    var totalC = (quantity*0.30); 
    document.getElementById("totc").value = totalC; 

}); 

JSFiddle

0

尝试使用此代码

function calcRowWash() { 
     var x = document.forms[0]['quantc'].value; 
     var quantity = 0; 
     if (x != "") { 
      quantity = parseInt(x); 
     } 
     var totalC = (quantity * 0.30); 
     document.forms[0]['totc'].value = totalC.toString(); 

    } 

HTML标记,我已经改变了隐藏式为文本框和它为我工作。

<td width = "90px" align ="left"><input type = "text" id ="quantc" tabindex = "13" onblur="calcRowWash()"/></td> 
<td width = "90px" align ="left"><input type = "text" id ="totc" tabindex = "13"/></td> </div> 
0
function calcRowWash(){ 
    var theForm = document.forms["orderform"]; 
    var x = theForm.getElementById("quantc").value; 
    var quantity = 0; 

    // Check if it is not empty 
    if(x.value != ""){ 
     // Check if it is a valid number 
     if(x/x == 1){ 
      quantity = parseInt(x.value); 
     } 
    } 
    var totalC = (quantity * 0.30); 
    document.getElementById("totc").value = totalC; 
} 

这对我的作品。