gpt4 book ai didi

php - Jquery 在 Yii2 中不工作

转载 作者:行者123 更新时间:2023-12-01 07:09:45 26 4
gpt4 key购买 nike

这是我的代码 https://jsfiddle.net/tusharj/0hoh109k/1/

这在 jsfiddle 中完美工作,但是当我在 yii2 中使用此代码时,它不起作用

_form.php

 <?= $form->field($model, 'relation')->checkboxList($relation, $options =   ['class' => 'check']);} ?>

这是上述表单的 html 代码,上面的代码在检查元素中如下所示

<div id="employeedetails-relation" class="check">
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="1"> Father</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="2"> Mother</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="3"> Spouse</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="4"> Child1</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="5"> Child2</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="6"> Father-in-law</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="7"> Mother-in-law</label></div></div>

下面的代码是在yii2中使用jquery

<?php

$this->registerJs('

$(document).ready(function () {
$(\'#employeedetails-relation\').on(\'change\', \':checkbox\', function () {
var value = parseInt($(this).val());

var checkedEl = [];
$(\':checkbox:checked\').each(function () {
checkedEl.push(parseInt($(this).val()));
});

console.log(checkedEl);
$(\'#employeedetails-relation :checkbox\').prop(\'disabled\', false);
console.log(value);
console.log($.inArray(value, checkedEl));
if ($.inArray(1, checkedEl) > -1 || $.inArray(2, checkedEl) > -1) {
$(\':checkbox[value=6]\').prop(\'disabled\', true);
$(\':checkbox[value=7]\').prop(\'disabled\', true);
} else if ($.inArray(6, checkedEl) > -1 || $.inArray(7, checkedEl) > -1) {

$(\':checkbox[value=1]\').prop(\'disabled\', true);
$(\':checkbox[value=2]\').prop(\'disabled\', true);
}

});

});', \yii\web\View::POS_READY);

?>

我不知道为什么它在 yii2 中不起作用

最佳答案

试试这个,它有效

namespace frontend\modules\test\assets;

use yii\web\AssetBundle;

class YourAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = '@frontend/modules/test/assets';

/**
* @inheritdoc
*/
public $publishOptions = [
'forceCopy' => YII_DEBUG,
];

/**
* @inheritdoc
*/
public $js = [
'js/your-file.js',
];

/**
* @inheritdoc
*/
public $depends = [
'yii\web\JqueryAsset',
];
}

关于php - Jquery 在 Yii2 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30321074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com