Input field selection with Jquery

Asked

Viewed 109 times

0

all right?

Can you give me a help related to the selection of text typed by a user in a form? I’ve tried some solutions I found here, but they didn’t work. I’ve also tried different approaches with pure Javascript, but I haven’t succeeded.

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

The sent function Data() is with the attribution/sending information for the controller. Debugging I can see that the function is being successfully called, but the variables that should be with the entered values 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();

From now on, thank you very much.

  • 1

    Hello Bruno, try to capture the values with this method : (var dataName = $("#name"). val(); ).

  • It worked perfectly, thank you very much. :)

  • Welcome Bruno, don’t miss a tour of the site. https://answall.com/tour Mark reply as accepts https://i.stack.Imgur.com/jx7Ts.png Why mark reply as accepts https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-acceptuma-resposta/1079#1079

1 answer

0

With Jquery

  • .val() - is a jQuery method mainly used to obtain the values of form elements such as input, select and textarea.
  • .text() - is a jQuery method whose output is a string containing the texto combinado of all elements within the referenced element.
  • .html - is a jQuery method used to obtain 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>
    
    <div style="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 the 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>

<div id="texto1"></div>
<div id="texto2"></div>
<input id="idInp" size="34">

Javascript-enabled

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

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

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

Browser other questions tagged

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