通过单击按钮数组获取隐藏 html 元素值的当前数组

Get current array of hidden html element value on the click of array of buttons

本文关键字:数组 元素 html 隐藏 单击 按钮 获取      更新时间:2023-09-26

我有html元素为:

<input type=hidden class=txtCustomerId value=".parent::current()." />";
<input type=button class=printToTextarea value='Get to box' />

和jquery:

$(".printToTextarea").on("click", function () {
    var array = $('.txtCustomerId').map(function() {
    return this.value;
}).get();
     loadxmldoc(array);
 });

它将所有元素作为数组从带有类名的隐藏字段中传递txtCustomerId而我只需要单击按钮时的当前元素。按钮也是数组,两者应具有相同的索引。

以下使用 eq()index() 的代码在很大程度上满足了要求。

$(".printToTextarea").on("click", function () {
    var i = $('.printToTextarea').index(this);
    var custid=$('.txtCustomerId').eq(i).val();
     loadxmldoc(custid);
    $("#textInput").focus();
 });

更改:

$('.txtCustomerId')

自:

$(this).prev('.txtCustomerId')

好吧,您正在选择所有元素。因此,您需要选择相关的一个。在您的示例中,您将使用 prev() 来获取对该元素的引用。

$(".printToTextarea").on("click", function () {
    var button = $(this);
    var inputValue = button.prev(".txtCustomerId").val();
    console.log(inputValue);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=hidden class=txtCustomerId value="hello" />
<input type=button class=printToTextarea value='Get to box' />

但是你如何获得输入实际上取决于你的HTML。因此,如果结构与彼此相邻的两个元素不同,则选择它的方式就会改变。