使用 JavaScript 更改输入值

change input value using javascript

本文关键字:输入 JavaScript 使用      更新时间:2023-09-26

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

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

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

脚本:

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>    

感谢您的帮助!

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

这是多余的。ID 在整个文档中是唯一的,因此这就足够了:

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

另请注意,我删除了.value - 这就是问题所在,因为您随后尝试获取值...的值。

这有效。

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.

尝试使用此代码

    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>
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;
}

这对我有用。

<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;
}

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

您可以使用以下 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;

利亚姆好运,祝你有美好的一天。