Get the value in an input text box

前端 未结 12 742
你的背包
你的背包 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:38

    You have to use various ways to get current value of an input element.

    METHOD - 1

    If you want to use a simple .val(), try this:

    
    

    Get values from Input

    // use to select with DOM element.
    $("input").val();
    
    // use the id to select the element.
    $("#txt_name").val();
    
    // use type="text" with input to select the element
    $("input:text").val();
    

    Set value to Input

    // use to add "text content" to the DOM element.
    $("input").val("text content");
    
    // use the id to add "text content" to the element.
    $("#txt_name").val("text content");
    
    // use type="text" with input to add "text content" to the element
    $("input:text").val("text content");
    

    METHOD - 2

    Use .attr() to get the content.

    
    

    I just add one attribute to the input field. value="" attribute is the one who carry the text content that we entered in input field.

    $("input").attr("value");
    

    METHOD - 3

    you can use this one directly on your input element.

    $("input").keyup(function(){
        alert(this.value);
    });
    

提交回复
热议问题