社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

在元素上单击获取子元素的文本,并使用jquery或纯javascript将其传递到输入字段中

Designer • 4 年前 • 1867 次点击  

根据我下面代码中的标题,我有4个div,其中包含4个不同的数字。我想要的是,当我点击每个div时,在输入字段中插入的点击div的数量。我的问题是,当我点击任何一个div时,它会传递输入字段中的所有金额。这是我的代码:

$(".btn-amount").each(function() {
  $(this).click(function() {
    var btnCreditsAmount = $(".hero-text").text();
    $('#amountInput').val(btnCreditsAmount);
  });
});
.btn-amount {
  min-width: 80px;
  text-align: center;
  padding: 0.5rem;
  border: 1px solid #ccc;
  float: left;
  margin: 5px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  background: #f2f4f8;
}

.form-group {
  display: block;
  clear: both;
  width: 360px;
  overflow: hidden;
  position: relative;
}

.input-field {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />

<div class="btn-amount">
  <div class="hero-text">10</div>
</div>

<div class="btn-amount">
  <div class="hero-text">20</div>
</div>

<div class="btn-amount">
  <div class="hero-text">40</div>
</div>

<div class="btn-amount">
  <div class="hero-text">80</div>
</div>

<div class="form-group">
  <input type="number" id="amountInput" class="input-field" placeholder="Amount of credits">
</div>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129737
文章 [ 2 ]  |  最新文章 4 年前
Tushar
Reply   •   1 楼
Tushar    4 年前

你不需要 .each 回路,你可以连接 click 处理每个 .btn-amount 找到 .hero-text 并插入 Inputbox .

$(this).find(".hero-text").text(); 将给出点击按钮的数量。所以你的点击处理程序应该是这样的

$(".btn-amount").click(function() {
    var btnCreditsAmount = $(this).find(".hero-text").text();
    $('#amountInput').val(btnCreditsAmount);
});

工作代码如下

$(".btn-amount").click(function() {
    var btnCreditsAmount = $(this).find(".hero-text").text();
    $('#amountInput').val(btnCreditsAmount);
});
.btn-amount {
  min-width: 80px;
  text-align: center;
  padding: 0.5rem;
  border: 1px solid #ccc;
  float: left;
  margin: 5px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  background: #f2f4f8;
}

.form-group {
  display: block;
  clear: both;
  width: 360px;
  overflow: hidden;
  position: relative;
}

.input-field {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />



<div class="btn-amount">
  <div class="hero-text">10</div>
</div>

<div class="btn-amount">
  <div class="hero-text">20</div>
</div>

<div class="btn-amount">
  <div class="hero-text">40</div>
</div>

<div class="btn-amount">
  <div class="hero-text">80</div>
</div>

<div class="form-group">
  <input type="number" id="amountInput" class="input-field" placeholder="Amount of credits">
</div>
freedomn-m
Reply   •   2 楼
freedomn-m    4 年前

你的台词

$(".hero-text")

将选择所有英雄文本元素。你需要限制被点击的那个。

在事件处理程序中,当使用 function() { , this 是已单击的元素。

所以那条线会变成 $(this).text() -然而,当使用 .text() 它包含了所有的空白,所以如果你的html有任何形式的布局,它会给出一个错误(就像它可能从一开始就出现的那样) \n 因此,当传递给。瓦尔)

相反,使用

var btnCreditsAmount = this.innerText;

更新的代码片段:

$(".btn-amount").each(function() {
  $(this).click(function() {
    var btnCreditsAmount = this.innerText;
    $('#amountInput').val(btnCreditsAmount);
  });
});
.btn-amount {
  min-width: 80px;
  text-align: center;
  padding: 0.5rem;
  border: 1px solid #ccc;
  float: left;
  margin: 5px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  background: #f2f4f8;
}

.form-group {
  display: block;
  clear: both;
  width: 360px;
  overflow: hidden;
  position: relative;
}

.input-field {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />



<div class="btn-amount">
  <div class="hero-text">10</div>
</div>

<div class="btn-amount">
  <div class="hero-text">20</div>
</div>

<div class="btn-amount">
  <div class="hero-text">40</div>
</div>

<div class="btn-amount">
  <div class="hero-text">80</div>
</div>

<div class="form-group">
  <input type="number" id="amountInput" class="input-field" placeholder="Amount of credits">
</div>