javascript - 根据选定的值启用按钮

标签 javascript jquery html ajax google-apps-script

公平警告我有 super 垃圾代码。话虽如此,我从一开始就禁用了按钮,以减少提交表单时发生的人为错误。我需要在填充三个特定字段时启用它。然而,这些字段之一是 html 中显示秒表的 header 标记。然后,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++;
        }
    }

最佳答案

尝试使用“更改”事件监听器,并测试是否应在每次更改时启用或禁用该按钮。

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); 
}

关于javascript - 根据选定的值启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531598/

相关文章:

javascript - 页面在加载自定义 javascript 时停止响应

javascript - 使用 ajax 发送输入文件数据

jquery - 带 Bootstrap 的两列 Accordion

html - 使用 css 隐藏 div 内容不起作用

javascript - 单击 <a> 时隐藏 <p> 并显示 &lt;textarea&gt;

javascript - Express'response.sendFile() 发送未捕获的语法错误 : Unexpected token <

javascript - 单击方法仅在纯 JavaScript 中不选择 contenteditable 元素

javascript - jQuery : Append jquery variable to a class name

xml - HTML 文档中是否需要 XML 声明?

javascript - 在 jquery 中按两个按钮将显示此操作