Get the value in an input text box

前端 未结 12 581
你的背包
你的背包 2020-11-22 07:54

What are the ways to get and render an input value using jQuery?

Here is one:

相关标签:
12条回答
  • 2020-11-22 08:13

    You can simply set the value in text box.

    First, you get the value like

    var getValue = $('#txt_name').val();
    

    After getting a value set in input like

    $('#txt_name').val(getValue);
    
    0 讨论(0)
  • 2020-11-22 08:17

    You can get the value like this:

    this['inputname'].value
    

    Where this refers to the form that contains the input.

    0 讨论(0)
  • 2020-11-22 08:19

    You can only select a value with the following two ways:

    // First way to get a value
    value = $("#txt_name").val(); 
    
    // Second way to get a value
    value = $("#txt_name").attr('value');
    

    If you want to use straight JavaScript to get the value, here is how:

    document.getElementById('txt_name').value 
    
    0 讨论(0)
  • 2020-11-22 08:21

    I think this function is missed here in previous answers:

    .val( function(index, value) ) 
    
    0 讨论(0)
  • 2020-11-22 08:25

    You can get the value attribute directly since you know it's an <input> element, but your current usage of .val() is already the current one.

    For the above, just use .value on the DOM element directly, like this:

    $(document).ready(function(){
      $("#txt_name").keyup(function(){
        alert(this.value);
      });
    });
    
    0 讨论(0)
  • 2020-11-22 08:26

    Pure JS

    txt_name.value
    

    txt_name.onkeyup = e=> alert(txt_name.value);
    <input type="text" id="txt_name" />

    0 讨论(0)
提交回复
热议问题