Problem picking textbox value with jQuery

Asked

Viewed 378 times

1

I have a Function that needs to take the value of the textbox to perform a validation and if this validation is true enable datepicker. But it’s not taking value from the textbox. By giving an Alert in the textbox, it is showing "Undefined". How can I solve ?

Script:

$(document).ready(function () {
  $("#txtNovaDtVenc").datepicker("option", "disabled", true,  { changeMonth: true, changeYear: true }).attr('readonly', 'readonly');
  $("#EscolhaData").hide();
  $('.a').click(function () {
    $("#EscolhaData").toggle();
    $('button').click(function () {
      var senha;    
      senha = $("txtSenha").val();    
      alert(senha);
      if (senha == "administrador") {
        $("#txtNovaDtVenc").datepicker({ changeMonth: true, changeYear: true }).attr('readonly', 'readonly');
        $("#LinhaSenha").hide();
      }
    });
  });
});

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="EscolhaData">
  <tr>
    <td>
      Nova dt. de vencimento :
    </td>
    <td>
      @Html.TextBox("txtNovaDtVenc", "", new { @class = "form-control form-control-custom", style="width:100px" })
    </td>              
  </tr>
  <tr id="LinhaSenha">
    <td>
      Senha: 
    </td>
    <td>
      @Html.TextBox("txtSenha", "", new { @class = "form-control form-control-custom", style="width:100px" })
    </td>
    <td>
      <button type="button" value="Ok" style="width:30px; height:30px;"></button>
    </td>
  </tr>
</table>
  • How are you giving the alert?

  • Mark the question as answered...

2 answers

5

To get the value of textbox with jquery would be like this:

$("#txtSenha").val();

4


Your field PASSWORD is like this:

@Html.TextBox("txtSenha", "", new { @class = "form-control form-control-custom", style="width:100px" })

You did not give an individual ID for this field. You can add a class .senha or a @id = 'senha', for example, and use this on your jQuery selector.

In your jQuery you’re doing so:

$("txtSenha").val();

But it won’t work because you’re not calling anything. It’s a dial that doesn’t exist.

Do this. In this case you are calling the selector with password field name.

var senha = $("input[name='txtSenha']").val();

Or if you add the class or ID you can call from your selector.

Class

var senha = $(".senha").val();

or ID

var senha = $("#senha").val();

  • 1

    It worked!! Thank you.

  • Mark the answer...

Browser other questions tagged

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