简体   繁体   English

onclick提交按钮未返回javascript函数yii

[英]onclick submit button not returning javascript function yii

My code is in the 我的代码在

view 视图

<?php echo CHtml::SubmitButton('Submit',array('onclick'=>'return validation();'));?>

and my script function is 我的脚本功能

function validation(){

    jQuery("#firstname_result").hide();

    jQuery("#lastname_result").hide();

    jQuery("#email_result").hide();

var firstname =jQuery("#first_name").val();

    var lastname = jQuery("#last_name").val();

    var email = jQuery("#email").val();

success=true;

if(firstname==''){

    success=false;

    jQuery('#firstname_result').html('First Name cannot be blank.').show();


}
if(lastname==''){
        success=false;
    jQuery('#lastname_result').html('Last Name cannot be blank.').show();

}
if(email==''){    
        success=false;
    jQuery('#email_result').html('Email cannot be blank.').show();

}

if(!success)
        return;

}

after clicking submit button, shows error and flashed out to other page where submit is targeted..can any one show me the way how I can able to overcome from this? 单击提交按钮后,显示错误并闪烁到提交目标所针对的其他页面。.任何人都可以向我展示如何克服这一问题的方法吗?

Try returning the value of the success variable. 尝试返回成功变量的值。 it should do the trick. 它应该可以解决问题。

if(!success) return;

should be 应该

return success;

this will stop the form from sumitting. 这将阻止表格的报价。

Use preventDefault to prevent form submitiion. 使用preventDefault可以防止表单提交。

Modified code: 修改后的代码:

<?php echo CHtml::SubmitButton('Submit',array('onclick'=>'return validation(event);'));?>

function validation(event){
   ..... //code 
  if(!success) {  
         event.preventDefault();
         event.returnValue = false; // for IE.
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM