简体   繁体   中英

Get the input value of the last input field using JQuery

Is there a way to get the last value of input text fields inside the menus class?

The values are dynamic and the number of input fields are also dynamic.In this example we have 4 input fields inside the menus class and the last value I want to get is 4.

<p class="menus">
    <input type="text" name="test" class="test" value="1">
    <input type="text" name="test" class="test" value="2">
    <input type="text" name="test" class="test" value="3">
    <input type="text" name="test" class="test" value="4">
</p>

You can use jquery last-child-selector

 function getValue() { console.log($(".menus input:last-child").val()) }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="menus"> <input type="text" name="test" class="test" value="1"> <input type="text" name="test" class="test" value="2"> <input type="text" name="test" class="test" value="3"> <input type="text" name="test" class="test" value="4"> </p> <button type="button" onclick="getValue()">Get Value</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM