2012-05-27 73 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; 
} 

這對我的作品。