用于排除.each上的字符串输入的Javascript

Javascript to exclude string input on .each

本文关键字:字符串 Javascript 输入 排除 each 用于      更新时间:2023-09-26

我正在为一些示例代码添加功能,但遇到了一些困难。原始代码是一个警报,我添加了包含消息的选项。以下是相关的片段。完整代码在这里http://codepen.io/stmoule/pen/WwJymN.

相对HTML

<div class="overlay">
  <div id="alarm-dialog">
    <h2>Set alarm after</h2>
    <label class="hours">
                Hours
                <input type="number" id="inputTimeValue" value="0" min="0" />
            </label>
    <label class="minutes">
                Minutes
                <input type="number" id="inputTimeValue" value="0" min="0" />
            </label>
    <label class="seconds">
                Seconds
                <input type="number" id="inputTimeValue" value="0" min="0" />
            </label>
<!--     Begin where I added -->
    <div class="messageBox">
      <label class="message">
                    Message
                    <input type="text" name="message" placeholder="Include a message?" />
                </label>
    </div>
<!--     End of my addition -->
    <div class="button-holder">
      <a id="alarm-set" class="button blue">Set</a>
      <a id="alarm-clear" class="button red">Clear</a>
    </div>
    <a class="close">X</a>
  </div>
</div>

相对Javascript

alarm_set.click(function() {
    var valid = true,
      after = 0,
      to_seconds = [3600, 60, 1];
    dialog.find('input').each(function(i) {
      // Using the validity property in HTML5-enabled browsers:
      if (this.validity && !this.validity.valid) {
        // The input field contains something other than a digit,
        // or a number less than the min value
        valid = false;
        this.focus();
        return false;
      }
      after += to_seconds[i] * parseInt(parseInt(this.value));
    });
    if (!valid) {
      alert('Please enter a valid number!');
      return;
    }
    if (after < 1) {
      alert('Please choose a time in the future!');
      return;
    }
    alarm_counter = after;
    dialog.trigger('hide');
  });

我相信JS正在查找所有的输入,这在我添加文本输入之前非常棒。我尝试添加一个for循环,将其限制在前三个,尝试添加'&amp;isNaN'到JS中的if语句,尝试在对话框.fund('input')中使用ID的/names。虽然我在谷歌上搜索了这个并在这里进行了广泛的搜索,但我觉得我已经在脑海中想出了解决方案,但在正确应用它时遇到了问题。

与其试图阻止.each对文本输入不做任何操作,不如尝试使用jQuery:not()选择器完全不选择它们。而不是说

$('input').each() 

你可以说

$('input:not(:text)');

https://api.jquery.com/not-selector/