Yii:当搜索字段为空并按下提交时显示消息 [英] Yii: show message when the search filed is empty and submit is pressed

查看:23
本文介绍了Yii:当搜索字段为空并按下提交时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <?php $roles=Rights::getAssignedRoles(Yii::app()->user->Id);foreach($roles 作为 $role){if($role->name == 'Admin'){?><!--直到这里完成,如果用户是管理员,将显示视图,如果不是管理员只会显示我们写在底部的消息,我们为用户填写志愿者表格所做的相同--><?php$this->breadcrumbs=array('志愿者表格'=>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'=>'你确定要删除这个项目吗?')),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' =>大批('类' =>'table table-striped table-condensed table-hover',),'数据'=>$模型,'属性'=>数组('ID','队名','name_of_ngo','电子邮件','地址','no_of_members','经验','用户身份',),));?><?php }if($role->name=='Authenticated'){?><br><br><br><br><br><br><br><br><br><br><div class="alert alert-success"><strong>恭喜!</strong>您已报名参加志愿服务,我们将尽快与您联系......!!

<?php}}?>

请帮我解决这个问题,谢谢.

解决方案

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

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

I am a Yiibie, and I want that when my search filed is empty and submit button is pressed it shows me the message on my screen to fill the field first. Here is code for the search bar

   <?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

        }     
}
?>

Please help me with this, thank you.

解决方案

You need JQuery and the id of the fields you need to check (change YourIdOfTheFieldToEval with the right value)

You can find the id in FireFox pressing CTRL +u and inspectt the source page code.. or via .. firebugs.

<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>

这篇关于Yii:当搜索字段为空并按下提交时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆