【问题标题】:jquery selector of all text ends with [duplicate]所有文本的jquery选择器都以[重复]结尾
【发布时间】:2017-10-24 22:19:30
【问题描述】:

我有一个在所有行中都有输入的表,我需要检查所有输入是否都填充了值 我尝试使用选择器 $("[id$='txtBarcode']") 来选择所有以 txtBarcode 结尾的输入,但它似乎不起作用。 怎么了?

$(document).ready(function() {
  $("#cmdConferma").click(function() {
    AllFilled();
  });
});

function AllFilled() {
  $("[id$='txtBarcode']").each(function() {
    $this = $(this);
    // var value = $("[id$='txtBarcode']").value();
    if ($this.value() == '') {
      return confirm('are you sure to exit without fill?');
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table_style" cellspacing="0" rules="all" border="1" id="dgrRighe" style="width:100%;border-collapse:collapse;">
  <tr>
    <td>
      <input name="dgrRighe$ctl02$txtBarcode" type="text" value="1" id="dgrRighe_ctl02_txtBarcode" />
    </td>
    <td>
      <input name="dgrRighe$ctl03$txtBarcode" type="text" id="dgrRighe_ctl03_txtBarcode" />
    </td>
    <td>
      <input name="dgrRighe$ctl04$txtBarcode" type="text" id="dgrRighe_ctl04_txtBarcode" />
    </td>
  </tr>
</table>
<input type="submit" name="cmdConferma" value="exit" id="cmdConferma" class="button" />

【问题讨论】:

    标签: jquery


    【解决方案1】:

    那是因为.value() 不是有效的jquery 方法。您需要改用.val()

    if ($this.val() == '')
    

    如果您的目标是获取元素的值,则无需创建元素 jquery 对象。您可以使用纯 JavaScript。我还建议使用trim() 来防止用户输入空字符:

    if (this.value.trim() == '')
    

    【讨论】:

    • .value() 也不是 javascript 中的方法。它是一个属性。对于使用.value,无需将 dom 对象转换为 jquery 对象。使用this.value 将返回值
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2013-05-05
    • 2014-06-29
    • 2018-08-16
    • 2011-06-27
    相关资源
    最近更新 更多