Javascript 排除 .each 上的字符串输入
Javascript to exclude string input on .each
我正在努力为一些示例代码添加功能,但遇到了一些困难。原始代码是一个警报,我正在添加包含一条消息的选项。这是相关的片段。完整代码在这里 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 循环,将其限制为前三个,尝试将 '&& isNaN' 添加到 JS 中的 if 语句,尝试使用 ID's/names 到 dialog.find('input' ).虽然我用谷歌搜索并在这里进行了广泛搜索,但我觉得我已经在脑海中想出了解决方案,但在正确应用它时遇到了问题。
与其试图阻止您的 .each 不对文本输入做任何事情,不如尝试使用 jQuery :not() 选择器完全不选择它们。而不是说
$('input').each()
你可以说
$('input:not(:text)');
我正在努力为一些示例代码添加功能,但遇到了一些困难。原始代码是一个警报,我正在添加包含一条消息的选项。这是相关的片段。完整代码在这里 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 循环,将其限制为前三个,尝试将 '&& isNaN' 添加到 JS 中的 if 语句,尝试使用 ID's/names 到 dialog.find('input' ).虽然我用谷歌搜索并在这里进行了广泛搜索,但我觉得我已经在脑海中想出了解决方案,但在正确应用它时遇到了问题。
与其试图阻止您的 .each 不对文本输入做任何事情,不如尝试使用 jQuery :not() 选择器完全不选择它们。而不是说
$('input').each()
你可以说
$('input:not(:text)');