如何预先以特定类前缀开头的所有输入

时间:2017-10-26 12:38:48

标签: javascript jquery

我有多个输入:

<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_1 cant_be_empty" type="text" onchange="doMultiplication(1)" onkeyup="doMultiplication(1)">
<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_2 cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)">

我想循环遍历它们并获取值。这是我的代码:

$("input[class^='rate_']").each(function() {
    alert('hi');
    totalTime = timeSum(totalTime, $(this).val());
});

它无法正常工作。有什么想法吗?

请注意,这些字段是由AJAX调用生成的。

3 个答案:

答案 0 :(得分:6)

由于它们是由AJAX调用生成的,因此最好在success回调上发送它们。如果不可行,您可以使用$.ajaxComplete()

$( document ).ajaxComplete(function() {
  $("input[class^='rate_']").each(function() {
    alert('hi');
    totalTime = timeSum(totalTime, $(this).val());
  });
});

此处的问题还在于,类并不总是以名称开头。所以你需要使用:

$("input[class^='rate_'], input[class*=' rate_']")

您的最终代码是:

$( document ).ajaxComplete(function() {
  $("input[class^='rate_'], input[class*=' rate_']").each(function() {
    alert('hi');
    totalTime = timeSum(totalTime, $(this).val());
  });
});

答案 1 :(得分:1)

对输入使用name属性而不是class,如下所示:

$("input[name^='rate_']").each(function() {
    console.log($(this).val())
    //totalTime = timeSum(totalTime,$(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_1 cant_be_empty" type="text" onchange="doMultiplication(1)" onkeyup="doMultiplication(1)" value="Value 1" name="rate_1">
<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_2 cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)" value="Value 2" name="rate_2">

答案 2 :(得分:0)

这不起作用,因为您的input中有几个班级。

&#13;
&#13;
$("input").each(function() {

   console.log($(this).attr("class"));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_1 cant_be_empty" type="text" onchange="doMultiplication(1)" onkeyup="doMultiplication(1)">
<input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_2 cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)">
&#13;
&#13;
&#13;

为您提供两种选择:

  1. 只检查输入$("input").each;检索class属性;拆分;并检查它是否包含课程rate_
  2. &#13;
    &#13;
    $("input").each(function() {
    var exist=false;
    var classes = $(this).attr('class').split(' ');
        $.each(classes,function(index,value){
         if(value.indexOf("rate_") !== -1){
         exist=true;
         }
       });
       console.log(exist);
    });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_1 cant_be_empty" type="text" onchange="doMultiplication(1)" onkeyup="doMultiplication(1)">
    <input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_2 cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)">
    <input class="pdf-in pdf-bg custom-inbox pdf-bg-3  cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)">
    &#13;
    &#13;
    &#13;

    1. 如果input属性中存在"rate_"子字符串,请选择class
    2. &#13;
      &#13;
      $("input[class*='rate_']").each(function() {
          alert('exist');
         
      });
      &#13;
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_1 cant_be_empty" type="text" onchange="doMultiplication(1)" onkeyup="doMultiplication(1)">
      <input class="pdf-in pdf-bg custom-inbox pdf-bg-3 rate_2 cant_be_empty" type="text" onchange="doMultiplication(2)" onkeyup="doMultiplication(2)">
      &#13;
      &#13;
      &#13;