Problem getting textbox value with jQuery

1

I have a function that needs to get the value of the textbox to perform a validation and if this validation is true enable the datepicker. But when not taking value from the textbox. When giving an alert in the textbox, it is showing "Undefined". How can I resolve?

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><tableid="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>
    
asked by anonymous 28.08.2015 / 21:11

2 answers

4

Your PASSWORD field looks 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 .senha or a @id = 'senha' class, for example, and use that in your jQuery selector.

In your jQuery you're doing it like this:

$("txtSenha").val();

But it will not work, because you're not calling anything. It is a selector that does not exist.

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

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

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

Class

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

or ID

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

    
28.08.2015 / 21:20
5

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

$("#txtSenha").val();
    
28.08.2015 / 21:20