簡體   English   中英

如何合並輸入框的值Javascript

[英]How to merge the value of input boxes Javascript

我有 5 個輸入框,每個輸入框都有不同的值 .. 我如何合並輸入框的值並進行比較,然后提醒或做一些事情..

例子:

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

//合並后將單詞與SAMPLE進行比較;

您已經標記了 jQuery,所以我假設您可以使用它。

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

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

JSFiddle

ES6:

const vals = $('input').map((_, elem) => elem.value).get().join('');

您可以使用 jQuery 中的each()方法來完成

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

小提琴演示

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

你可以試試這個。

var inputs = document.getElementsByTagName("input"),
    inputValue = "";
    for (var index=0; index < inputs.length; index ++ ) {
        inputValue += inputs[index].value;
    }
    if (inputValue === "SAMPLE") {
         //do something
    }
var result = $('input[type="text"]').map(function(i, e){
    return e.value;
}).get();

//do compare stuff on result

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM