Yii2在视图中使用if else _Form

时间:2017-03-24 06:32:03

标签: php jquery forms checkbox yii2-basic-app

我有2个名为Address和Mail_address的文本输入

我使用Jquery所以当我在Address中输入一些文本时,Mail_address与Address的文本完全相同。

现在我要添加1个复选框,这样当我点击该复选框时, Mail_address已被禁用或只读,并且它与地址

具有相同的值

但是如何为Jquery或_form中的复选框实现if else?

谢谢

这是我的观点_Form

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\BaseHtml;
use app\assets\myassets\InputAsset;
?>
<?php
$button = Html::button('Cancel', ['class' => 'btn btn-default btn-md', 'data' => ['dismiss' => "modal"]]);
$button .= Html::button('Submit', ['id' => 'submit', 'class' => 'btn btn-success success btn-md']);
Modal::begin([
    'header' => '<h2>Konfirmasi</h2>',
    'toggleButton' => false,
    'id' => 'confirm-submit',
    'footer' => $button
	]);
	echo 'Save ?';
Modal::end();
?>
<?php
$this->registerJs('

$(document).ready(function () {
		$(document).on("change" ,"#'. Html::getInputId($model ,'Address') .'" ,function(){
        $("#'. Html::getInputId($model ,'Mail_Address') .'").val();
		var first = $("#'. Html::getInputId($model ,'Address') .'").val();
        var third = first;
        $("#'. Html::getInputId($model ,'Mail_Address') .'").val(third);
		});
		
		
});


');

?>
<div class="tcust-form">

    <?php $form = ActiveForm::begin([
		'id' => 'marketing',
	]); 
	?>
	<div class="row">
        <div class="col-md-6">
			<?= $form->field($model, 'Address')->textInput() ?>
			<?= $form->field($model, 'Mail_Address')->textInput() ?>
			<?= $form->field($model, 'SameasAbove')->checkbox() ?>
		</div>	
	</div>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

1 个答案:

答案 0 :(得分:0)

您需要做的就是为

复选框编写更改功能
$('#checkboxID').on('change',function(){
if($('#checkboxID).prop('checked'))
{
 var address = $('#addressID').val();
$('#mailAddress').val(address).addClass('disabled');
}
else
{
if($('#checkboxID).hasClass('disabled')
{
$('#mailAddress).removeClass('disabled');
}

}

})