当前位置: 首页 > 知识库问答 >
问题:

按类名获取输入字段的值,其中类名是可变的

蒋高杰
2023-03-14

我有许多对文本字段和提交按钮,提交按钮的id和文本字段的class相同,但每对都不同。所以我想把按钮点击后在文本字段中输入的值传递给ajax函数。

null

function update_rate(id) {
  // var price = document.getElementsByClassName(id)[0].innerHTML;

  var price = document.getElementsByClassName(id);
  console.log(price);
  $.ajax({
    type: "POST",
    url: "update_rate.php", // Name of the php files
    data: {
      subcategory: id,
      price: price
    },
    success: function(res) {
      // console.log(html);
      alert(res);
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
first pair:
<div class="form-group">
  <input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="a" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>

<div class="form-group">
  <input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="b" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>

null

但是我无法将文本字段的值转换为变量。

共有3个答案

晏树
2023-03-14

几件事

var price=document.GetElementsByClassName(id);是复数,您需要该值,因此

var price=Document.GetElementsByClassName(id)[0].value;如果必须

但它不是一个类。这是一个名字

var price=document.GetElementsName(id)[0].value;如果必须

但是如果您有jQuery,为什么不使用它呢?

这里,我取按钮ID并按名称查找输入

我还更改为type=“button”-当您使用Ajax时,您不希望提交

null

$(function() { // on page load
  $("[type=button]").on("click", function() { // click on type="button" you can use a Class here too
    const id = $(this).attr("id");
    const price = $("[name="+id+"]").val(); // get the input by name
    console.log(id, price)
    if (price) {

      $.ajax({
        type: "POST",
        url: "update_rate.php", // Name of the php files
        data: {
          subcategory: id,
          price: price
        },
        success: function(res) {
          // console.log(html);
          alert(res);
        }
      });
    }
  })

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


first pair:
<div class="form-group">
  <input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="button" id="a" class="btn btn-primary">Update</button>

<div class="form-group">
  <input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="button" id="b" class="btn btn-primary">Update</button>
逑阳泽
2023-03-14

由于您已经在函数中传递了id,因此可以使用jQuery执行如下操作:

var selector = 'input[name="' + id + '"]' // Compose the selector string for jQuery
var value = $(selector).val()             // Get input value
束帅
2023-03-14

这可以而且应该以一种不同的,更简单的方式来完成。通过idclass将按钮与其文本字段连接起来不是一个可伸缩的解决方案,需要不断更新代码以保持所有命名的同步。只需获取按钮之前的文本字段的值。如果您修改HTML结构,使文本字段和按钮一起位于相同的div中,这将更容易。

  • 不要使用内联事件处理程序,而是将事件处理代码分离到JavaScript中。
  • 在较高的DOM元素上只设置一个事件处理程序,并在它冒泡到该元素时处理它。这称为事件委派。
  • 使用data-*属性在元素中存储自定义数据。
  • 另外,在2020年不要使用.getElementsByClassName()。相反,使用.querySelector.

见下文评论。

null

// Do your event handling in JavaScript, not with inline HTML event handling attributes
// Also, set up just one handler at a parent level of the items that might trigger
// the event (event delegation).
document.querySelector(".wrapper").addEventListener("click", update_rate);

function update_rate(event){
  // See if it was a submit button that got clicked
  if(event.target.classList.contains("btn")){
    // A submit button was pressed.
    // Locate the nearest ancestor element that has the form-group class
    // and then, from there, find the first input (which is the one you want).
    var input = event.target.closest(".form-group").querySelector("input");
    console.log(input.value);
    $.ajax({
      type: "POST",
      url: "update_rate.php", // Name of the php files
      // Use the dataset API to extract the custom attribute on the input
      data: {subcategory : input.dataset.category , price: input.value},
      success: function(res){
        alert(res);
      }
    });
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="form-group">
    <input type="text" class="form-control" data-category="a" placeholder="Your task rate excl. taxes">
    <button type="submit" class="btn btn-primary">Update</button>
  </div>

  <div class="form-group">
    <input type="text" class="form-control"  data-category="b" placeholder="Your task rate excl. taxes">
    <button type="submit" class="btn btn-primary">Update</button>
  </div>
</div>
 类似资料:
  • 问题内容: 我仅使用Javascript创建表单。我正在尝试使用Javascript获取位于中的输入字段的值。是否有可能获得内的字段的值? 问题答案: 是的,即使该站点来自另一个域,也应该可行。 例如,在我网站的HTML页面中,我有一个iFrame,其内容来自另一个网站。iFrame内容是单个选择字段。 我需要能够在我的网站上读取所选的值。换句话说,我需要使用我自己的应用程序中另一个域的选择列表。

  • 问题内容: 我有一个类,我想找到它的所有 公共字段 (不是方法)。我怎样才能做到这一点? 谢谢! 问题答案: 返回该类的所有公共变量的数组。 返回整个类继承中的字段。如果要仅在相关类中定义字段,而不在其超类中定义字段,请使用,并通过以下方法过滤它们: 的字面实际上代表类型的对象。检查其文档以获取更多有趣的反射方法。 上面的类是。您可以查看整个程序包。

  • 这里Book是根类,即最终的CSV将命名为books.CSV。 使用,我们只获得字段名title和author,但是我们还需要来自author类的字段名(name和age),有没有方法获得这些字段名?

  • 我想从传递的几个值动态调用几个“setter”方法。每个方法都将具有字符串,int...要设置的变量类型(例如:setUserName(String userName)或setUserAge(int age))。 在我的例子中,我有一个setter方法“setUse_status(int use_stats)”,我无法配置getDeclaredMethod方法工作。似乎在获取方法时,classVa

  • 本文向大家介绍postgresql 实现获取所有表名,字段名,字段类型,注释,包括了postgresql 实现获取所有表名,字段名,字段类型,注释的使用技巧和注意事项,需要的朋友参考一下 获取表名及注释: 过滤掉分表: 加条件 and relchecks=0 即可 获取字段名、类型、注释、是否为空: 补充:PostgreSQL查询表主键及注释内容 网上关于pgSql获取表主键的内容都是千篇一律,并

  • 快速编辑:正如下面指出的,我应该做BFS,但我需要一个点来停止检索新字段,我还没有时间考虑。谢谢你的帮助! 我试图使用Java反射递归地获取类的字段,本质上创建一个树来显示 这是一个递归解析器,所以我认为递归显示函数是很酷的。不幸的是,它压垮了我。 示例类Foo,具有Class1和Class2字段,每个字段可以有更多不同类的字段: 我的递归方法: println已经用于测试,Foo的一个示例输出(

  • 问题内容: Java中的类定义了用于检查给定参数是否与某些Unicode字符相等或属于某种类型类别的方法。这些字符和类型类别已命名。 由于在给定的javadoc说,对于名为字符的例子是 ,,…; 例如名为类型类别 ,… 但是,作为或值而不是枚举,这些类型的名称在运行时被“隐藏”。 因此, 是否有可能在运行时获取字符和/或类型类别的名称? 问题答案: JDK7将有一个 函数(READ:类java.l

  • 问题内容: 我需要在其他班上变数。我怎样才能做到这一点? 所以我尝试在Textcl.class中使用,但是得到了。 问题答案: 您将得到空值,因为inString从未按Robert Kilar在注释中正确指出的那样进行初始化。 您可以通过使用类名来引用静态变量。 示例ClassName.variablename。就你而言 运行您的主类。当您运行inString时,将在该类的构造函数中对其进行初始化