[英]How to Disable a button in Yii2
I'm trying to disable the Create Project Button
when the user is not logged in, the button will Hide
or disable
.我试图在用户未登录时禁用“创建项目”
Button
,该按钮将Hide
或disable
。
And this is my condition:这是我的条件:
<p>
<?php
if (Yii::$app->user->isGuest) {
Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs']);
} elseif(Yii::$app->user->identity->username) {
Html::a('Create a Project', ['create'], ['class' => 'btn btn-success']);
}
?>
</p>
It's working, But, when the user is logged in, the button is already Hide!它正在工作,但是,当用户登录时,按钮已经隐藏了!
How can disable or hide the button in Yii2 and fix that problem?如何禁用或隐藏 Yii2 中的按钮并解决该问题?
is there any tutorial about that?有没有关于那个的教程?
You need to add a disabled
attribute to disable the button, or to hide it completely you can use CSS style=display: none;
你需要添加一个
disabled
属性来禁用按钮,或者完全隐藏它你可以使用 CSS style=display: none;
Both are used in the code below两者都在下面的代码中使用
<p>
<?php
if (Yii::$app->user->isGuest) {
// This button will be displayed, but is disabled
Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs', 'disabled' => 'disabled']);
} elseif(Yii::$app->user->identity->username) {
Html::a('Create a Project', ['create'], ['class' => 'btn btn-success']);
} else {
// This button will not be displayed (it is hidden)
Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs', 'style' => 'display: none;']);
}
?>
</p>
First of all you cannot disable a
tag.首先,你不能禁用
a
标签。 disabled
attribute works fine on Button
tags eg: disabled
属性在Button
标签上工作正常,例如:
<?= Html::Button('Project', ['class' => 'btn btn-success', 'disabled' => Yii::$app->user->isGuest ]) ?>
If you really want to disable a
tag then you can use this example:如果你真的想禁用
a
标签,那么你可以使用这个例子:
HTML: HTML:
<a id="a1" href="http://www.google.com">Google 1</a>
Javascript: Javascript:
$('#a1').attr('disabled', 'disabled');
$('a').on('click', function(e) {
if ($(this).attr('disabled') == 'disabled') {
e.preventDefault();
}
});
If you are just checking for logged
in user then Use !Yii::$app->user->isGuest
and you forget echo
:如果您只是检查
logged
用户,那么使用!Yii::$app->user->isGuest
并且您忘记echo
:
if (!Yii::$app->user->isGuest) {
echo Html::a('Create a Project', ['create'], ['class' => 'btn btn-primary btn-xs'])
}
试试这个代码:
<?= Html::a('Create a Project', ['create'],['class' =>'btn btn-primary disabled']); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.