javascript - 根据显示的复选框显示警报

标签 javascript jquery function validation checkbox

我正在构建一个药物提醒应用程序,它的工作原理是用户单击一个按钮,系统将根据当前时间显示用户应该服用的药物。假设用户是否应该在下午 3 点服用 Med A 和 Med B,当用户在下午 3 点单击按钮时,会弹出 Med A 和 Med B 两个复选框,但如果用户只选中“Med A”,系统应该显示一条消息询问“你为什么不服用 Med B?”带有一个保管箱,允许用户选择原因。

但是目前,如果用户只选中“Med A”,系统将显示消息询问“为什么不服用 Med B 和 Med C”,因为 Med C 也被列为复选框的选项之一,只是它没有按应在不同时间显示的方式显示。因此,无论用户在任何特定时间应该服用什么药物,系统都会始终根据所有复选框选项进行询问,而不是根据当前显示的复选框(根据时间显示)进行询问。

JS:

function validate() {
        var msg = [];
        [].forEach.call(document.querySelectorAll('input[type="checkbox"]:not(:checked)'), function(elem, index) {
                        msg.push(elem.name);
                        });
                        navigator.notification.alert(msg.length ? 'Why didnt you take ' + msg.join(' and ') : 'Well done, you have taken all the medications!', alertDismissed, 'Message for you:','Done');

        }

    function showDiv(){
        if(document.querySelectorAll('input[type="checkbox"]:not(:checked)').length==0) {
            hide();
        }else{document.getElementById('welcomeDiv').style.display = "block";}
    }

    function myFunction(){
        var hour = (new Date()).getHours();
        showMed('A', hour == 15);
        showMed('B', hour == 15);
        showMed('C', hour == 19);
    }

    function showMed(med, show) {
        document.getElementById('med' + med).style.display = show ? '' : 'none';
    }

HTML

<div class="inner" id=text><button onClick="myFunction()" >Check</button></div>

</div>
    <div id=aa style="display:none">
        <form>
            <div id='medA'>
                <input type="checkbox" name="Med A" value="A">Medication A
            </div>
            <div id='medB'>
                <input type="checkbox" name="Med B" value="B">Medication B
            </div>
            <div id='medC'>
                <input type="checkbox" name="Med C" value="C">Medication C
            </div>

                <div id="welcomeDiv" style="display:none;" class="dropdown" title="Basic dialog">
                    <select>
                        <option value="" disabled="disabled" selected="selected">Please choose one</option>
                        <option value="forget">Forget to take</option>
                        <option value="notfeeling">Not feeling like taking it</option>
                        <option value="sideeffect">Worried about side-effects</option>
                        <option value="sideeffect">Run out of supplements</option>
                        <option value="misclick">Misclick</option>
                        <option value="others">Others</option>
                    </select>
                        <input id="btnbutton" class="btn" type="button" onClick="know();hide()" value="Submit">
                </div>
            <input id=xbutton type="button" onClick="validate();showDiv()" value="Submit">
        </form>

最佳答案

如果未显示该复选框,您可以将该名称添加到药物列表中。只需更改一个代码(如下所示)就足以修复它。

var valid = true;

function validate() {
        var msg = [];
        [].forEach.call(document.querySelectorAll('input[type="checkbox"]:not(:checked)'), function(elem, index) {
                        msg.push(elem.name); valid = false;
                        });
 if(!valid) {
                        navigator.notification.alert(msg.length ? 'Why didnt you take ' + msg.join(' and ') : 'Well done, you have taken all the medications!', alertDismissed, 'Message for you:','Done'); }

        }
}

function showDiv(){
          if(valid) {
            hide();
        }else{document.getElementById('welcomeDiv').style.display = "block";}
}

关于javascript - 根据显示的复选框显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38630300/

相关文章:

javascript - 预期 onClick 监听器是一个函数,而不是类型对象 - React redux

javascript - 我如何关注以声明方式提交的第一个无效表格?

jquery - 图像要放置取右:0px,但不取

javascript - 了解函数如何使用和存储变量

javascript - 使用箭头函数和 addEventListener 更改元素的字体大小和文本颜色

javascript - 使用 ECMAScript 国际化 API 格式化日期和时间

javascript - Javascript 中使用菊花链三元处理数组和平均值的解决方案?

javascript - 添加/删除现有表单字段而不丢失事件

php - jQuery 手机 : Two/three column vertical control group of radio buttons

python - 从函数列表中获取随机函数并调用所选函数