[英]Enabling a button based on selected values
一般警告,我有超级垃圾代码。 话虽如此,我从一开始就禁用了按钮,以减少提交表单时发生的人为错误。 当填充三个特定字段时,我需要启用它。 但是,这些字段之一是html中的标头标记,用于显示秒表。 然后,jQuery函数将输出作为文本抓取,并将其存储到名为userInfo的对象中。 当ID任务,源和输出不为空白/ =“ 00:00:00”时,我想启用我的提交按钮。
我在不同的地方尝试了多个if语句来检查输入值的有效性。 但是,对于最新的if语句,当默认值为“ disabled”时,它将启用按钮
<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++;
}
}
尝试使用“更改”事件侦听器,并测试是否应在每次更改时启用或禁用按钮。
document.addEventListener("change", validate);
// I need the output for task,source,and the text value for output to not be blank/"00:00:00" for the button to be enabled.
function validate() {
const ready = $("#task").val() && $("#source").val() && $("#output").text() !== "00:00:00";
$("#btn").prop("disabled", ! ready);
}
通读,我相信您想根据select#source
的值将button#btn
disabled
属性更改为enabled
。 您可以使用下面的示例代码来实现
// 1st: get select#source and button#btn using their ID
var source = document.getElementById('source');
var button= document.getElementById('btn');
// 2nd : capture source value on change using JS Event Listener
source.addEventListener("change", function() {
if(source.value == "some source") {
// 3rd : remove disabled attribute and set enabled attribute
button.removeAttribute("disabled");
button.setAttribute("enabled", "");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.