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

javascript confirm function return false - Stack Overflow

programmeradmin3浏览0评论

I want to show a message which writing are you sure … I can show the message but user click NO return false; did not work

<script>
function askCar(){
  var answer = confirm ("Are you sure for deleting car ?");
  if (answer){

  }
  else{
    return false;
  }
}
</script>

I call function with onclick here

echo '<td><a onclick="askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';

I want to show a message which writing are you sure … I can show the message but user click NO return false; did not work

<script>
function askCar(){
  var answer = confirm ("Are you sure for deleting car ?");
  if (answer){

  }
  else{
    return false;
  }
}
</script>

I call function with onclick here

echo '<td><a onclick="askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';
Share Improve this question edited Jan 6, 2014 at 13:10 elclanrs 94.1k21 gold badges136 silver badges171 bronze badges asked Jan 6, 2014 at 13:10 Gökhan ÇokkeçeciGökhan Çokkeçeci 1,3983 gold badges16 silver badges37 bronze badges 1
  • 4 You know you can do negative conditionals? eg. if (!answer) return false – elclanrs Commented Jan 6, 2014 at 13:11
Add a ment  | 

4 Answers 4

Reset to default 11

You need to use the return value:

<a onclick="return askCar();" ... >

Just having it inside the onclick will execute the function but to actually cancel the click, you need to use the return value.

Also, the code can be slightly optimized:

function askCar(){
    return confirm ("Are you sure for deleting car ?");
}

No need in if..else when you can directly return the dialog result. (unless of course you want to perform exra actions)

You need to add the return also:

echo '<td>
    <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a>
    <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a>
    </td>';

Your onclick function needs to use the return value:

echo '<td>
         <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">
            Sil
         </a>
         <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">
             Düzenle
         </a>
      </td>';

When you use return within your function definition, you must have to use return in the place where you are calling that. Otherwise, you will not get proper result. So, the best code would be below

echo '<td><a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';
发布评论

评论列表(0)

  1. 暂无评论