Selectedindex does not work

Asked

Viewed 62 times

0

I am validating a small form and can’t do it using the property selectedIndex.

Code that is not working:

const movimento_loja = window.document.getElementById('movi')


function camposPreenchidos() {
    if (objetivo_mensal.value.length == 0) {
        objetivo_mensal.focus()
        return false
    } else if (valor_venda.value.length == 0) {
        valor_venda.focus()
        return false
    } else if (movimento_loja.selectedIndex == 0) {
        return false
    } else {
        return true
    }
}
<select name="mv" id="movi">
    <option value="movimento">Movimento da loja hoje</option>
    <option value="ninguem">Ninguém entrando</option>
    <option value="gato-pingado">Só gato-pingado</option>
    <option value="normal">Fluxo normal</option>
    <option value="fluindo">Tá fluindo</option>
    <option value="explodiu">Explodiu!</option>
</select>

I could only solve the problem using the property value of movimento_loja, thus:

function camposPreenchidos() {
    if (objetivo_mensal.value.length == 0) {
        objetivo_mensal.focus()
        return false
    } else if (valor_venda.value.length == 0) {
        valor_venda.focus()
        return false
    } else if (movimento_loja.value == 'movimento') {
        return false
    } else {
        return true
    }
}

I would like to understand better why, because, even though I have solved the problem, I feel I have not fully understood the reason for the error.

  • What is objetivo_mensal?

  • I tested here and the property selectIndex works normally. Try to detail your answer a little more so that we can reproduce the problem.

  • @Luizfelipe is an input type Number. As well as selling value_value. However store is a combobox that comes with default value 'Store movement today' and the user must switch to a valid value. Equals an empty field, only to prevent the user from sending a valid default value without actually choosing.

No answers

Browser other questions tagged

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