2016-11-04 36 views
0

好,所以我拋出了一個項目,並且我的JS經驗有限。我正在嘗試製作一個腳本,它能夠獲得一些單選按鈕單擊和文本輸入的結果,並將它們放在一起,以便將它們導向到一個IP上。 我的代碼有點工作哈哈,但我知道必須有更乾淨的方式。 當我不記得我如何嘗試這樣做,但它涉及document.getelementbyname(「loc).value,但它返回undefined ..現在,我試圖做同樣的事情與」港口「無線電我想做的事情正確的。對不起,如果我搞砸這個職位了。單選按鈕傳遞的值有問題

<label><input type="radio" name="loc" value="16" onclick= "clt();" />Carolinas</label><br> 
<label><input type="radio" name="loc" value="17" onclick= "geo();"/>Georgia</label><br> 
<label><input type="radio" name="loc" value="18" onclick= "sxw();"/>Xpress</label><br><br> 


<label><input type="number" placeholder="Store Number" id="thirdOct" /> </label> 
<label><input type="radio" name="cwPort" value="90"/>Port 90</label> 
<label><input type="radio" name="cwPort" value="91"/>Port 91</label> 
<label><input type="radio" name="cwPort" value="92"/>Port 92</label> 
<br>  
<input type="submit" class="fsSubmitButton" value="Connect" onclick="makeIp();" /> 



var ipAdd; 
var sNum = "0"; 
var int = [":", 81 ]; 

// had to go this route for a value, because when I try to call by document.getElementByName("loc") in my makeIp function it returns undefined for the value of "loc" 





function clt() 
{sNum = "16";} 


function geo() 
{sNum = "17";} 

function sxw() 
{ sNum = "18";} 

function makeIp() 
{ 
var storeN = document.getElementById("thirdOct").value; 
var octThree; 
if (storeN >= 300 && storeN <= 399) { 

      octThree = storeN.slice(-2); 
     if (octThree < 10) { 
      octThree = storeN.slice(-1); 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0] ,"_blank"); 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0]); 
          } 
     else { 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0] ,"_blank"); 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0] ); 
            }  

          } 
else { 

     if (storeN >= 300) { 
      octThree = storeN.slice(-2); 

      if (octThree < 10) { octThree = storeN.slice(-1); 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 

           } 

      else { 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
         } 
      } 
     else { octThree = storeN; 
     var ipAdd = "http://172." + sNum + "." + octThree + ".65" + int[0] + int[1] ;  
      console.log(ipAdd); 
     window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
     console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 

     } 
    } 
} 

所以我發現了一個可能的解決方案使用下面的代碼來代替我曾打電話CLT,地理位置和SXW功能....

var radios = document.getElementsByName('genderS'); 

for (var i = 0, length = radios.length; i < length; i++) { 
if (radios[i].checked) { 
    // do whatever you want with the checked radio 
    alert(radios[i].value); 

    // only one radio can be logically checked, don't check the rest 
    break; 
} 
} 
+0

您可以使用'document.querySelector(」 [name =「genderS」]:選中').value;'獲取選中的單選按鈕的值。 –

回答

0

我在回答字段中發佈這個,所以我可以告訴你代碼,但我不太確定這是你的答案,因爲你沒有發佈所有相關的原始代碼。

我修改了您的代碼以擺脫內聯HTML事件處理程序併合並冗餘代碼。我也糾正幾個錯誤你在哪裏針對一些比較字符串值:

storeN >= 300 && storeN <= 399 

octThree < 10 

var rads = document.querySelectorAll(input[type=radio]); 
 
for(var i = 0; i < rads.length; ++i){ 
 
    rads[i].addEventListener("click", setVal); 
 
} 
 

 
var btn = document.querySelector(input[type=button]); 
 
btn.addEventListener("click", makeIp); 
 

 
var sNum = 0; 
 
var int = [":", 81 ]; 
 
var ipAdd = null; 
 

 
function setVal(){ 
 
    sNum = parseInt(this.value, 10); 
 
} 
 

 
function makeIp() { 
 
    var strNum = document.getElementById("thirdOct").value; 
 
    var storeNum = parseInt(strNum, 10); 
 
    var octThree = null; 
 
    
 
    if (storeNum >= 300 && storeNum <= 399) { 
 
     octThree = parseInt(strNum.slice(-2), 10); 
 
     if (octThree < 10) { 
 
      octThree = strNum.slice(-1); 
 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0]); 
 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0]); 
 
          } 
 
     else { 
 
      window.open("http://172." + sNum + "." + octThree + ".65" + int[0]); 
 
      console.log("http://172." + sNum + "." + octThree + ".65" + int[0]); 
 
     }  
 

 
    } else { 
 

 
     if (storeNum >= 300) { 
 
      parseInt(strNum.slice(-2), 10); 
 

 
      if (octThree < 10) { 
 
       octThree = storeN.slice(-1); 
 
       window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
       console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
      } else { 
 
       window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
       console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
      } 
 
    } else { 
 
    octThree = storeN; 
 
    var ipAdd = "http://172." + sNum + "." + octThree + ".65" + int[0] + int[1] ;  
 
    console.log(ipAdd); 
 
    window.open("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
    console.log("http://172." + sNum + "." + octThree + ".65" + int[0] + int[1],"_blank"); 
 
    } 
 
}
<label><input type="radio" name="loc" value="16">Carolinas</label><br> 
 
<label><input type="radio" name="loc" value="17">Georgia</label><br> 
 
<label><input type="radio" name="loc" value="18">Xpress</label><br><br> 
 

 
<label><input type="number" placeholder="Store Number" id="thirdOct" /> </label> 
 
<label><input type="radio" name="cwPort" value="90">Port 90</label> 
 
<label><input type="radio" name="cwPort" value="91">Port 91</label> 
 
<label><input type="radio" name="cwPort" value="92">Port 92</label> 
 
<br>  
 
<input type="button" class="fsSubmitButton" value="Connect">