Can you place an array in a Javascript variable and compare it to the value in the text field?

Asked

Viewed 1,274 times

1

I have the code below and need to get several different values at once. One way I thought was array no longer works. After Ajax he brings me the result in id zpl, if the result comes empty it returns the warning that does not have the value in the bank. So far so good, only that I would also like if the result comes SD10 and SD11 and so on, also appear the same message of the empty condition, however I wish I could not put several ous || in condition and place the values inside an array variable and make the comparison in the

var variavel = new array('valor1','valor2');
document.getElementById('zpl').value == variavel;

Can someone help me?

I would like to take several values and put inside a variable and at the time of validating with the command document.getElementById('valor do campo').value == the variable that is with these values. I tried:

var arr = new array("s10","s11","s12","s13");

document.getElementById('valor do campo').value = arr

That is, it will compare the value of the page’s text field with the values of the variables stored in the variable arr .

Does anyone know how to do this in Javascript?

$.ajax({
    type: "POST",
    url: "label_query.php",
    data: {
        serial: $('#serial').val(),
        combox: $("#combox").val()
    },
    success: function(data) {
        $('#zpl').html(data);

        var arr = new array('SD10', 'SD11', 'SD12');

        if (document.getElementById('zpl').value == '' || document.getElementById('zpl').value == arr) {

            alert('NÂO HÁ REGISTRO DESSE SERIAL NO BANCO DE DADOS!');
            document.getElementById('serial').value = '';
            document.getElementById("bSubmit").disabled = false;
            document.getElementById("bSubmit").value = "Enviar";
            document.getElementById("serial").focus();
            return false;
        } else {
            criarArquivo();
            document.getElementById("bSubmit").disabled = false;
            document.getElementById("bSubmit").value = "Enviar";
            document.getElementById("serial").focus();
        }
  • use http://api.jquery.com/jquery.each/

  • What gives console.log(data); in the first line of success: function(data) {?

3 answers

1

I would use only this way:

['s10','s11','s12','s13'].indexOf(document.getElementById('zpl').value) !== -1;

The result of this line would be true if the field value is any of these in the array, otherwise it would be false

1

You can use the indexOf as follows:

function contem() {
  var valor = document.getElementById("valor").value;
  var arr = ['ASD', 'DEF', 'GHI'];
  document.getElementById('result').innerHTML = (arr.indexOf(valor) > -1 ? 'Contem o valor digitado' : 'Nao Contem');
}
<input type="text" id="valor">
<button onclick="contem()">Testar</button>
<div id="result"></div>

A very important detail to mention is that this architecture could be better planned, I explain:
When backend does not find a record, it should ALWAYS return an error and never a success.
Hence you wouldn’t have to treat different solutions in equal corners. It’s just an improvement tip.

0

You must transform the array into a string and then compare it with the value

Substitute:

var arr = new array("s10","s11","s12","s13");

For

var arr = ['s10', 's11', 's12', 's13'].join("");

So

document.getElementById('zpl').value == arr;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.