简体   繁体   English

使用 JQuery 获取最后一个输入字段的输入值

[英]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?有没有办法获取menus类中输入文本字段的最后一个值?

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.值是动态的,输入字段的数量也是动态的。在这个例子中,我们在menus类中有 4 个输入字段,我想获得的最后一个值是 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您可以使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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