Input field selection with JQuery

0

Alright?

Can you give me a help related to the selection of text entered by a user on a form? I have tried some solutions that I found here, but they did not work. I've also tried different approaches with pure Javascript, but I was not successful.

The form is inside an Asp.Net MVC view and I need to get the data entered by the user on the page to send to the Controller.

The dispatchDatas () function has the assignment / dispatch information for the controller. Debugging I can see that the function is being called successfully, but the variables that should be with the values entered are "null".

Form:

  <p class="text-center">Dados para cadastro: </p>
            <form>
                <div class="col-md-12 form-group">
                    <label for="nome">Nome: </label>
                    <input type="text" class="form-control" id="nome" placeholder="Nome.">
                </div>
                <div class="col-md-12 form-group">
                    <label for="email">E-mail: </label>
                    <input type="email" class="form-control" id="email" placeholder="E-mail.">
                </div>
                <div class="col-md-12 form-group">
                    <label for="telefone">Telefone: </label>
                    <input type="text" class="form-control" id="telefone" placeholder="Número de contato.">
                </div>
                <div class="col-md-12 form-group">
                    <button type="submit" class="botaogrande btn btn-primary" onclick="enviarDados()">Cadastrar</button>
                </div>
            </form>

Attempt to select with jQuery:

var dadosNome = $("#nome").text();
var dadosTelefone = $("#telefone").text();
var dadosEmail = $("#email").text();

Thank you in advance.

    
asked by anonymous 22.10.2018 / 00:24

1 answer

0

With Jquery

  • .val () - is a jQuery method primarily used to get form element values such as input, select, and textarea.
  • .text () - is a jQuery method whose result is a string containing texto combinado of all elements within the referenced element.
  • .html - is a jQuery method used to get the conteúdo HTML of the first referenced element
    console.log($("#divNome").html());
    console.log($("#divNome").text());
    console.log($("#nome").val());
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divstyle="display:none" id="divNome" class="col-md-12 form-group">
      <label for="nome">Nome: </label><span>Value do input já preenchido!</span>
      <input type="text" class="form-control" id="nome" placeholder="Nome." value="Bruno">
    </div>
                    
  

These methods can also be used to define the content of elements

$("#texto1").html('<a href="#">Este é um link com a função .html</a>');
 $("#texto2").text('<a href="#">Este é um link com a função .text</a>');
  $("#idInp").val('Este é value do input com a função .val');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="texto1"></div>
<div id="texto2"></div>
<input id="idInp" size="34">

With JavaScript

var valor = document.getElementById("nome").value;

console.log(valor);
 <input type="text" class="form-control" id="nome" placeholder="Nome." value="Bruno">
  

The property value sets or returns the value of the value attribute of a text field.

    
22.10.2018 / 03:19