gpt4 book ai didi

javascript - 按下按钮时获取位于倍数 div 内的表单

转载 作者:行者123 更新时间:2023-11-30 19:21:14 25 4
gpt4 key购买 nike

我是 Jquery 新手。

当我按下按钮时,我正在尝试获取属于表单一部分的对象。

我的问题是我有多个具有相同类“formulario”的表单,当我按下按钮时,Jquery 会给我所有具有相同类型类的表单,而我只想要按下按钮的表单。

我有这样的东西:

<div class ="card">
<div class = "card-header">
First Form
</div>

<div class= "card-body">
<form class="formulario">
<label>Input one</label>
<input type text= "text"></input>

<label>Input Two</label>
<input type text= "text"></input>
<form/>

<button class= "btn">Press</button>
<div/>
</div>



<div class ="card">
<div class = "card-header">
Second Form
</div>

<div class= "card-body">
<form class="formulario">
<label>Input one</label>
<input type text= "text"></input>

<label>Input Two</label>
<input type text= "text"></input>
<form/>

<button class= "btn">Press</button>
<div/>
</div>

我尝试将 Jquery 与这样的东西一起使用:

$('.btn').on("click", function () {
$(this).parents().find('.formulario');
});

它给了我所有具有“formulario”类的表单,但我只想要按下按钮的表单。

换句话说,我只想寻找按钮所在的 parent ,我不想在所有 html 文档中寻找。

最佳答案

为了获得最近的公式类,您可以使用 .closeset

$('.btn').on("click", function () {
$(this).closeset().find('.formulario');
});

关于javascript - 按下按钮时获取位于倍数 div 内的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com