【发布时间】:2019-08-16 21:58:45
【问题描述】:
公平警告我有超级垃圾代码。话虽如此,我从一开始就禁用了我的按钮,以减少提交表单时发生的人为错误的数量。当填充三个特定字段时,我需要启用它。但是,其中一个字段是显示秒表的 html 中的标题标记。然后,一个 jquery 函数将该输出作为文本抓取,并将其存储到一个名为 userInfo 的对象中。我想在 ID 任务、源和输出不为空/ = "00:00:00" 时启用我的提交按钮。
我在不同的地方尝试了多个 if 语句来检查输入值的验证。但是,对于最新的 if 语句,它会在默认为“禁用”时启用按钮
<div class ='row'>
<div class="input-field col s6">
<input type="text" id="task" class="autocomplete" placeholder ='Task' required>
<div class="input-field col s6">
<select id = 'source'>
<option value="" diasable selected></option>
<option value="some source">source1</option>
<option value="other source">source2</option>
</select>
<label>source1 or source2</label>
</div>
<h2 id = 'output'><b>0:00:00:00</b></h2>
<button id="start"class="waves-effect waves-light btn">start<i class="material-icons right">timer</i></button>
<button id="stop"class="waves-effect waves-light btn">stop<i class="material-icons right">timer_off</i></button>
<button id ="btn" class="btn waves-effect waves-light #26a69a" type="submit" name="action" disabled>Track It!
<i class="material-icons right">access_time</i>
</button>
我需要任务、源的输出和输出的文本值不为空白/“00:00:00”才能启用按钮。
这里是一些 javascript
M.toast({html: 'Record Submitted'})
var userInfo = {};
userInfo.task= document.getElementById('task').value;
userInfo.source= document.getElementById('source').value;
userInfo.timeSpent= $("#output").text();
userInfo.units= $("#count").text();
google.script.run.userClicked(userInfo);
document.getElementById('task').value='';
var source = document.getElementById('source');
source.selectedIndex = 0;
M.FormSelect.init(source);
}
var h1 = document.getElementById('output'),
start = document.getElementById('start'),
stop = document.getElementById('stop'),
//clear = document.getElementById('clear'),
seconds = 0, minutes = 0, hours = 0,
t;
function add() {
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
}
}
【问题讨论】:
-
为什么不使用
#btn的点击事件来验证所有三个条件并在满足时提交?
标签: javascript jquery html ajax google-apps-script