2014-02-10 46 views
-1

我有5個輸入框,每個輸入框有不同的值..我如何合併輸入框的值並比較它,然後提醒或做些什麼..如何合併輸入框的值Javascript

實施例:

<form> 
      <input id="input1" type="text"> 
      <input id ="input2" type = "text"/> 
      <input id ="input3" type = "text"/> 
      <input id = "input4" type = "text"/> 
      <input id = "input5" type = "text"/> 
</form> 

    var input1 = document.getElementById('input1').value; 
    var input2 = document.getElementById('input2').value; 
    var input3 = document.getElementById('input3').value; 
    var input4 = document.getElementById('input4').value; 
    var input5 = document.getElementById('input5').value; 
    var wordsConcat = input1.concat(input2,input3,input4,input5); 

//合併比較的話樣品之後;

+0

環路他們和讀出的值...你嘗試過什麼。合併意味着什麼? – epascarello

+0

如果出現在wordsconcat或整個concatinated單詞是樣本的意義上比較??? –

回答

1

可以使用each()方法做它在jQuery的

var a=''; 
$('input[type=text]').each(function(){ 
    a+=this.value; 
}); 
if (a === "SAMPLE") { 
    //do something 
} 

Fiddle Demo

0
var sample = 'your word'; 
if(wordsConcat === sample){ 
    alert('a'); 
} 
0

你可以試試這個。

var inputs = document.getElementsByTagName("input"), 
    inputValue = ""; 
    for (var index=0; index < inputs.length; index ++) { 
     inputValue += inputs[index].value; 
    } 
    if (inputValue === "SAMPLE") { 
     //do something 
    } 
5

您已經標記了jQuery,因此我假設它可用於您。

可以映射每個輸入到一個值,然後join()在一起使用空字符串:

var vals = $('input').map(function(){ 
    return this.value; 
}).get().join(''); 

JSFiddle

0
var result = $('input[type="text"]').map(function(i, e){ 
    return e.value; 
}).get(); 

//do compare stuff on result 
相關問題