简体   繁体   中英

How to merge the value of input boxes Javascript

I have 5 input box, and each input box Have a different value .. how i can merge the value of the input boxes and compare it, then alert or do something ..

Example:

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

//after merging Compare the words to SAMPLE;

You've tagged jQuery so I'm assuming it's available to you.

You can map each input to a value, and then join() them together using an empty string:

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

JSFiddle

ES6:

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

You can do it using each() method in jQuery

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

Fiddle Demo

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

You can try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM