1

Here is my code https://jsfiddle.net/tusharj/0hoh109k/1/

this working perfectly in jsfiddle, but while am using this code in yii2, its not working

_form.php

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

this is html code for above form, above code is looks like in below in inspect element

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

below code is used jquery in yii2

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

?>

I don't Know why its not working in yii2

2 Answers 2

3

Try This it works

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',
    ];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to have comments on downvotes. Please explain your(whomsoever) reason to downvote the answer. It will help me in improving my answer
1

Registering js code like that is considered bad practice. Use assets instead, it's even recommended in official docs.

Note that your code does not even depend on any php data.

Move it to separate file and include using assets.

Here is basic example:

<?php

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',
    ];
}

Registering asset bundle in view:

YourAsset::register($this);

For more information about assets, check official docs.

5 Comments

i did that too, but the result is same. Not Working
What do you mean by "not working"? What kind of error is in the browser console?
if i check value 1 in checkbox, value 6 and 7 checkbox not getting disable
So the error is related with logic, not the fact that the script executes before jQuery loads? If so, you should definetely mention such details in question.
In this case simply check if js code and generated HTML output are exactly the same as in jsfiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.