JQuery does not take input value

2

$(document).on('click','#submit',function(){
    console.log($('#prescricao[qtd][1]').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" name="prescricao" id="prescricao[qtd][1]" class="form-control input-md prescricao" autocomplete="off" placeholder="" tabindex="2"/>

<button name="Cadastrar" class="btn btn-success" id="submit">Cadastrar</button>
    
asked by anonymous 17.08.2017 / 17:46

1 answer

6

You have to escape [] within the string because it will be interpreted as a CSS selector and in selectors [] means attribute selector in HTML. So with \ before each [] will already give.

$(document).on('click', '#submit', function() {
  console.log($('#prescricao\[qtd\]\[1\]').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" name="prescricao" id="prescricao[qtd][1]" class="form-control input-md prescricao" autocomplete="off" placeholder="" tabindex="2" />

<button name="Cadastrar" class="btn btn-success" id="submit">Cadastrar</button>
    
17.08.2017 / 17:48