Yii:当搜索字段为空并且按下提交时显示消息


Yii: show message when the search filed is empty and submit is pressed

我是Yiibie,我希望当我的搜索字段为空并按下提交按钮时,它会在屏幕上显示消息,让我先填写字段。这是搜索栏的代码

   <?php
/* @var $this VolunteerFormController */
/* @var $model VolunteerForm */
?>
<?php $roles=Rights::getAssignedRoles(Yii::app()->user->Id);
foreach($roles as $role) 
{ 
if($role->name == 'Admin')
{?><!--till here is done so that if the user is admin the view will be shown and if it not an admin 
   only the message will be shown which we have written in the bottom, same we have done for the user fill the volunteer form-->

<?php
$this->breadcrumbs=array(
    'Volunteer Forms'=>array('index'),
    $model->id,
);
$this->menu=array(
    array('icon' => 'glyphicon glyphicon-list','label'=>'List VolunteerForm', 'url'=>array('index')),
    array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create VolunteerForm', 'url'=>array('create')),
    array('icon' => 'glyphicon glyphicon-edit','label'=>'Update VolunteerForm', 'url'=>array('update', 'id'=>$model->id)),
    array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete VolunteerForm', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
    array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage VolunteerForm', 'url'=>array('admin')),
);
?>
<?php echo BsHtml::pageHeader('View','VolunteerForm '.$model->id) ?>
<?php $this->widget('zii.widgets.CDetailView',array(
    'htmlOptions' => array(
        'class' => 'table table-striped table-condensed table-hover',
    ),
    'data'=>$model,
    'attributes'=>array(
        'id',
        'team_name',
        'name_of_ngo',
        'email',
        'address',
        'no_of_members',
        'experience',
        'user_id',
    ),
)); ?>
<?php }
if($role->name=='Authenticated')
{?>
 <br><br><br><br><br><br><br><br><br><br>
            <div class="alert alert-success">
  <strong>Congrats!</strong> You have signed up for Volunteering, You will be contacted soon..!!.
</div>
     <?php
        }     
}
?>

请帮我做这件事,谢谢。

您需要JQuery和需要检查的字段的id(用正确的值更改YourIdOfTheFieldToEval)

您可以在FireFox中按CTRL+u找到id并检查源页面代码。。或通过。。萤火虫。

<script type="text/javascript">
  $('form').submit(function () {
  // Get the value and trim it
   var field1 = $.trim($('#YourIdOfTheFieldToEval1').val());
   // Check if empty of not
   if (field1=== '') {
       alert('Text-field is empty.');
       return false;
   }
  // Get the value and trim it
   var field2 = $.trim($('#YourIdOfTheFieldToEval2').val());
   // Check if empty of not
   if (field2=== '') {
       alert('Text-field is empty.');
       return false;
   }
   // repeat for the fields you need to check 
   .......
 });
</script>