簡體   English   中英

確認框在javascript驗證中不起作用

[英]Cofirm box is not working in javascript validation

我正在使用Java腳本驗證處理表單。驗證有問題。確認框在特定實例下不起作用。 我的代碼是

if((frm_update_announcement.sublink[0].checked == true)) {
    if((document.getElementById('captionurl').value.trim()=="") && document.getElementById("captionfile").files.length == 0 ) ) {   
        if (document.getElementById('olddocument').value.trim()=="") {
            alert("Enter a url/upload a file");
            document.getElementById('captionurl').focus();
            return false;
        }
    }
}
if((frm_update_announcement.sublink[1].checked == true)) {
    for (var i = 1; i <= n; i++) {
        if (document.getElementById('attachment_caption' + i).value.trim()!="") {
            if ((document.getElementById('url'+i).value.trim()=="") && (document.getElementById("document"+i).files.length == 0 ) ) {
                alert("Add url/file with caption");
                document.getElementById('url'+i).focus();
                return false;   
            }                             
        }
    }         
}
if(confirm("Do you want to update the announcement") == true) {
    return true;
} else {
    return false;
}

我的HTML是

<label>
    <input type="radio" name="sublink" id="id_radio2" value="0" <?php if($announcements_details['detail_type']=="0") echo' checked="checked"'?> >&nbsp;Add attachment to title</input>
</label>
<div class="col-md-4">      
<label> 
    <input type="radio" name="sublink" id="id_radio1"  value="1" <?php if($announcements_details['detail_type']=="1") echo' checked="checked"'?> >&nbsp;Add new sublinks</input>
</label>
</div>
<div class="col-md-4">      
<label>
    <input type="radio" name="sublink" id="id_radio3" value="2" <?php if($announcements_details['detail_type']=="2") echo' checked="checked"'?>>&nbsp;None
</label>

當條件frm_update_announcement.sublink[1].checked變為true時,不會出現確認框。 那就是問題所在。 但是該功能內的警報框顯示正確。

function myFunction() {
var txt;
var r = confirm("Press a button!");
if (r == true) {
    txt = "You pressed OK!";
} else {
    txt = "You pressed Cancel!";
}
document.getElementById("demo").innerHTML = txt;
}

您的第一個if語句中有語法錯誤。

將以下代碼替換為您的第一個if語句。

 if ( ( frm_update_announcement.sublink[0].checked == true )){
        if ((document.getElementById('captionurl').value.trim()=="" )  && (document.getElementById("captionfile").files.length == 0) )
            {  
                if (document.getElementById('olddocument').value.trim()=="")
                 {
                    alert("Enter a url/upload a file");
                    document.getElementById('captionurl').focus();
                    return false;
                }
             }

}

希望這對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM