javascript确认框问题

时间:2016-05-19 09:11:26

标签: javascript

我在此链接上使用了确认框

<a href="<?php echo Yii::$app->params["url"]; ?>admin/index/deletcompany&companyid=<?php echo $Companydata['company_id']; ?>&userid=<?php echo $userid; ?>" id="<?php echo $Companydata['company_id']; ?>" onclick="confirmdialog()"><span class="glyphicon glyphicon-trash" title="Delete"></span></a>

Javascript代码

function confirmdialog(e){
     var r=confirm("Are you sure you want to delete this company?");
        if(r == true)
        {

         return true;
        }
        else
        {
         return false;
      }     

    }  

但是每个案例都会执行,然后点击取消。请给我提问。

2 个答案:

答案 0 :(得分:2)

不使用Onclick事件和自定义函数:

<a href="javascript:if(confirm('Are you sure you want to delete this company?')){ window.location='<?php echo Yii::$app->params["url"]; ?>admin/index/deletcompany&companyid=<?php echo $Companydata['company_id']; ?>&userid=<?php echo $userid; ?>'; };" id="<?php echo $Companydata['company_id']; ?>"><span class="glyphicon glyphicon-trash" title="Delete"></span></a>

通过为Onclick事件使用自定义函数:

<a href="<?php echo Yii::$app->params["url"]; ?>admin/index/deletcompany&companyid=<?php echo $Companydata['company_id']; ?>&userid=<?php echo $userid; ?>" id="<?php echo $Companydata['company_id']; ?>" onclick="return confirmdialog();"><span class="glyphicon glyphicon-trash" title="Delete"></span></a>

<script type="text/javascript">
    function confirmdialog(){
        return confirm('Are you sure you want to delete this company?');
    }
</script>

使用JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<a class="my_anchors" href="<?php echo Yii::$app->params["url"]; ?>admin/index/deletcompany&companyid=<?php echo $Companydata['company_id']; ?>&userid=<?php echo $userid; ?>" id="<?php echo $Companydata['company_id']; ?>"><span class="glyphicon glyphicon-trash" title="Delete"></span></a>

    <script type="text/javascript">
    $(document).ready(function(){
        $( ".my_anchors" ).click(function() {
          return confirm('Are you sure you want to delete this company?');
        });
    });
    </script>

答案 1 :(得分:1)

试试这个html onclick="return confirmdialog()"

<a href="delete.php" onclick="return confirmdialog()">delete</a>

和js

function confirmdialog(e){
 if(!confirm("Are you sure you want to delete this company?"){
     return false;
  }     

}