2013-04-23 125 views
0

我想添加+ 10%的值,如果該值被選中,但在我可以實現它之前,我需要使它正常工作,頁面的第一個不應刷新和當我添加一個值,然後我想改變它到另一個我想從另一個文本框的舊值減去...我嘗試了一個簡單的if/else if/else語句,但由於第一個錯誤它不會工作,我甚至不確定它是否正常工作。下面是代碼:錯誤下拉列表(JS)

<!doctype html> 

<html> 
<head> 
    <title>lol</title> 
    <meta charset="utf-8"> 

</head> 
<body> 
    <form action=""> 
     <input type="text" id="hp"> 
     <input type="text" id="ea"> 
     <input type="text" id="ed"> 
     <input type="text" id="pa"> 
     <input type="text" id="pd"> 
     <input type="text" id="sp"> 

     <select> 
      <option value="hp">HP</option> 
      <option value="ea">EA</option> 
      <option value="ed">ED</option> 
      <option value="pa">PA</option> 
      <option value="pd">PD</option> 
      <option value="sp">SP</option> 
     </select> 
     <input type="submit" id="sub" onClick = "return at()"> 
    </form> 

    <script type="text/javascript"> 
     function at() { 
      var a = document.form.select.options, 
       i = document.form.select.selectedIndex, 
       ea = Number(document.form.ea.value), 
       ed = Number(document.form.ed.value), 
       hp = Number(document.form.hp.value), 
       pd = Number(document.form.pd.value), 
       pa = Number(document.form.pa.value), 
       sp = Number(document.form.sp.value), 
       eaS,edS,pdS,paS,spS,hpS = 0; 

      if(a[i].text === "hp"){ 
       hpS = hp*0.1; 
      }else if(a[i].text === "ea"){ 
       eaS = ea*0.1 
      }else if(a[i].text === "ed"){ 
       edS = ed*0.1 
      }else if(a[i].text === "pa"){ 
       paS = pa*0.1 
      }else if(a[i].text === "pd"){ 
       pdS = pd*0.1 
      }else if(a[i].text === "sa"){ 
       spS = sp*0.1 
      } 

      document.form.ea.value = eaS; 
      document.form.ed.value = edS; 
      document.form.hp.value = hpS; 
      document.form.pd.value = pdS; 
      document.form.pa.value = paS; 
      document.form.sp.value = spS; 

      return false; 
     } 
    </script> 
</body> 
</html> 
+0

嘗試更改document.form到document.forms [0],你應該通過 – Satya 2013-04-23 12:28:12

+0

nope仍然不能正常工作 – aleksXPO 2013-04-23 12:32:20

回答

1

看看JavaScript控制檯

我願意打賭它有說像錯誤。如果您沒有看到該錯誤,請設置您的控制檯,以便在頁面導航中保持錯誤。

Uncaught TypeError: Cannot read property 'select' of undefined

您正在尋找名稱爲表單的元素和名稱爲select的元素,但您的表單/ select沒有名稱。

<form action=""> 
<select> 

應該

<form name="form" action=""> 
<select name="select"> 

理想情況下,你將放棄document.name.name語法和使用的getElementById。在選擇和表單上添加一個id並參考輸入。還有一個switch語句會成爲你的朋友。

您的代碼可以在沒有if語句的情況下編寫,只需使用基於選擇值的lookup。需要

function at() { 
    var sel = document.getElementById("mySelect"); 
    var val = sel.options[sel.options.selectedIndex].value; 
    var input = document.getElementById(val); 
    if (input) { 
     var val = input.value; 
     resetValues(); 
     input.value = parseFloat(val) * .1; 
    } 
} 

function resetValues() { 
    var inputs = document.getElementById("myForm").getElementsByTagName("input"); 
    for (var i=0;i<inputs.length;i++) { 
     var inp = inputs[i]; 
     if (inp.type==="text") inp.value = 0; 
    } 
} 

JSFiddle - Running example

+0

@ Xotic750謝謝,複製了錯誤的URL! – epascarello 2013-04-23 13:13:11

+0

OP的代碼也會顯示NaN – epascarello 2013-04-23 13:24:30

+0

好的,但是如何將值更改爲0而返回到以前的值呢? – aleksXPO 2013-04-23 13:34:01

0

許多變化:

<!doctype html> 

<html> 
<head> 
    <title>lol</title> 
    <meta charset="utf-8"> 

</head> 
<body> 
    <form action=""> 
     <input type="text" name="hp" id="hp"> 
     <input type="text" name="ea" id="ea"> 
     <input type="text" name="ed" id="ed"> 
     <input type="text" name="pa" id="pa"> 
     <input type="text" name="pd" id="pd"> 
     <input type="text" name="sp" id="sp"> 

     <select id="mySelect"> 
      <option value="hp">HP</option> 
      <option value="ea">EA</option> 
      <option value="ed">ED</option> 
      <option value="pa">PA</option> 
      <option value="pd">PD</option> 
      <option value="sp">SP</option> 
     </select> 
     <input type="button" id="sub" value="Submit" onClick = "return at()"> 
    </form> 

    <script type="text/javascript"> 
     function at() { 
      var a = document.getElementById('mySelect').options, 
       i = document.getElementById('mySelect').selectedIndex, 
       ea = Number(document.forms[0].ea.value), 
       ed = Number(document.forms[0].ed.value), 
       hp = Number(document.forms[0].hp.value), 
       pd = Number(document.forms[0].pd.value), 
       pa = Number(document.forms[0].pa.value), 
       sp = Number(document.forms[0].sp.value), 
       eaS,edS,pdS,paS,spS,hpS = 0; 

      if(a[i].text === "HP"){ 
       hpS = hp*0.1; 
      } 
      if(a[i].text === "EA"){ 
       eaS = ea*0.1 
      } 
      if(a[i].text === "ED"){ 
       edS = ed*0.1 
      } 
      if(a[i].text === "PA"){ 
       paS = pa*0.1 
      } 
      if(a[i].text === "PD"){ 
       pdS = pd*0.1 
      } 
      if(a[i].text === "SA"){ 
       spS = sp*0.1 
      } 

      document.forms[0].ea.value = eaS; 
      document.forms[0].ed.value = edS; 
      document.forms[0].hp.value = hpS; 
      document.forms[0].pd.value = pdS; 
      document.forms[0].pa.value = paS; 
      document.forms[0].sp.value = spS; 

      return false; 
     } 
    </script> 
</body> 
</html> 
0

像這樣的事情

<form action=""> 
    <input type="text" id="hp"> 
    <input type="text" id="ea"> 
    <input type="text" id="ed"> 
    <input type="text" id="pa"> 
    <input type="text" id="pd"> 
    <input type="text" id="sp"> 
    <select name="select"> 
     <option value="hp">HP</option> 
     <option value="ea">EA</option> 
     <option value="ed">ED</option> 
     <option value="pa">PA</option> 
     <option value="pd">PD</option> 
     <option value="sp">SP</option> 
    </select> 
    <input type="button" id="sub" value="Submit"> 
</form> 

var sub = document.getElementById("sub"); 

function at() { 
    var a = document.forms[0].select.options, 
     i = document.forms[0].select.selectedIndex, 
     ea = Number(document.forms[0].ea.value), 
     ed = Number(document.forms[0].ed.value), 
     hp = Number(document.forms[0].hp.value), 
     pd = Number(document.forms[0].pd.value), 
     pa = Number(document.forms[0].pa.value), 
     sp = Number(document.forms[0].sp.value), 
     eaS, edS, pdS, paS, spS, hpS; 

    eaS = edS = pdS = paS = spS = hpS = 0; 

    if (a[i].text === "HP") { 
     hpS = hp * 0.1; 
    } else if (a[i].text === "EA") { 
     eaS = ea * 0.1 
    } else if (a[i].text === "ED") { 
     edS = ed * 0.1 
    } else if (a[i].text === "PA") { 
     paS = pa * 0.1 
    } else if (a[i].text === "PD") { 
     pdS = pd * 0.1 
    } else if (a[i].text === "SA") { 
     spS = sp * 0.1 
    } 

    document.forms[0].ea.value = eaS; 
    document.forms[0].ed.value = edS; 
    document.forms[0].hp.value = hpS; 
    document.forms[0].pd.value = pdS; 
    document.forms[0].pa.value = paS; 
    document.forms[0].sp.value = spS; 

    return false; 
} 

sub.addEventListener("click", at, false); 

OB jsfiddle

我沒有固定的前夕你可能會遇到的問題是你的代碼,只是主要部分。你可以使用這個小提琴來解決你的其他問題。

+0

好的,但我怎麼能讓它的價值,如果它被更改爲0,但回到以前的價值? – aleksXPO 2013-04-23 13:32:58

+0

對不起,我不明白你的意思?這個問題是你問的原始問題的一部分嗎? – Xotic750 2013-04-23 13:35:49