最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript confirm() ok and cancel return same result - Stack Overflow

programmeradmin4浏览0评论

This is my code:

<input type="submit" value="Delete" onclick="del('Are you sure you want to Delete?')">

<script language="JavaScript">
function del(display) 
{   
    var inputs = document.myform;   

    if(inputs[5].value != '')
    {
        confirm(display);  
    }
}
</script>

When I click either the "ok" or the "cancel", they both did the same way (the OK way)

How can I make the "cancel" do what it is supposed to do?

NOTE: I have a form there that I didn't include for the simplicity of question.

This is my code:

<input type="submit" value="Delete" onclick="del('Are you sure you want to Delete?')">

<script language="JavaScript">
function del(display) 
{   
    var inputs = document.myform;   

    if(inputs[5].value != '')
    {
        confirm(display);  
    }
}
</script>

When I click either the "ok" or the "cancel", they both did the same way (the OK way)

How can I make the "cancel" do what it is supposed to do?

NOTE: I have a form there that I didn't include for the simplicity of question.

Share Improve this question edited Sep 4, 2012 at 7:31 Bijoy Thangaraj 5,5464 gold badges45 silver badges73 bronze badges asked Sep 4, 2012 at 7:23 Sam SanSam San 6,9039 gold badges35 silver badges51 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

change your code like this.

onclick="return del('Are you sure you want to Delete?')"

    if(inputs[5].value != '')
        {
            return confirm(display);  
        }
<input type="submit" value="Delete" onclick="return del('Are you sure you want to Delete?')">

<script language="JavaScript">
function del(display) 
{   
    var inputs = document.myform;   

    if(inputs[5].value != '')
    {
        return confirm(display);  
    }
}
</script>

you need to add two return in your code (both onclick attribute and inside del() function). You should also return something if your condition inputs[5].value != '' is false

Do it like this;

onclick="return del('Are you sure you want to Delete?')"
发布评论

评论列表(0)

  1. 暂无评论