If条件在javascript中不起作用

if condition does not work in javascript

本文关键字:不起作用 javascript 条件 If      更新时间:2023-09-26
function confirmappt(target) {
    var type = document.getElementById("ultrasoundid").value
    var ultrasound = document.getElementById("hdultrasoundid").value
    var appt = document.getElementById('apptdocfacid').value
    appt = 'apptid' + appt + '_1'
    var checkedValue = 'test'
    for (var i = 0; i < 100; i++) {
        if (document.getElementsByName(appt)[i].checked) {
            checkedValue = 'ok'
            break;
        }
    }
    if (checkedValue == 'ok') {
        alert('Please select ultrasound type!');
    }
    if (confirm('Are you sure you want to make this appointment?')) 
      return true;
    return false;
}

如果checkedValue等于ok,警报和第二个if条件都有效。但是checkedValue不等于ok,那么第二个if条件不工作。请帮我解决这个问题

您需要添加else

我认为你需要

if (checkedValue == 'ok') {
    alert('Please select ultrasound type!');
}else{  
    return confirm('Are you sure you want to make this appointment?');
}

我从你的问题陈述中了解到,如果checkValue是"ok",你想要警报,如果checkValue不是"ok",只确认对话框

使用

if (checkedValue == 'ok') {
        alert('Please select ultrasound type!');
    }else{
       if (confirm('Are you sure you want to make this appointment?')) 
      return true;

}