简体   繁体   中英

Need to be able to get the return string from javascript method in yii2

I am working on Yii2 and having a problem with returning the content of the javascript method to be sent when using the Html::a -hyperlink

The javascipt is in order to obtain the selected value of the checkboxes and it is outputting correctly.

 <script> function getNewPermissions() { var permissions = ''; var rows = $(document).find('.permission'); $.each(rows, function (key, value) { if($(value).find('input').prop('checked') == true) permissions += value.id+'$&/'; }) permissions = permissions.substring(0, permissions.lastIndexOf("$&/")); return permissions; } </script> 
 echo Html::a(Yii::t('app', 'Edit'), ['permissions/edit' ,'id'=> $name], [ 'class' => 'btn btn-primary', 'onclick' =>'js:getNewPermissions()', 'data-method' => 'post', 'data' => [ 'params' => ['newPerms'=>'js:getNewPermissions()','_csrf' => Yii::$app->request->csrfToken], ], ]) 

In the yii1 the value was read correctly from the params. Just cant find any source to help get js in the params directly and the onclick does work.

in my project use another way

<a style="float:left;" class="btn btn-success" onclick="myFunction(this)" 
               type="<?php echo $value->id; ?>">
                                </a>

and use js function in end of layout

<script>
            function updateSession(e)
            {
                var csrfToken = $('meta[name="csrf-token"]').attr("content");
                var pid = e.getAttribute('type');
                var key = e.getAttribute('title');
                var pqty = $("#id_" + key).val()

                $.ajax({
                    url: '<?php echo Yii::$app->urlManager->createAbsoluteUrl('/site/updatecard') ?>',
                    type: 'Post',
                    data: {
                        productid: pid,
                        key: key,
                        pqty: pqty,
                        _csrf: csrfToken
                    },
                    success: function (data) {

                        alert(data);
                        $.ajax({
                            url: '<?php echo Yii::$app->urlManager->createAbsoluteUrl('/site/getcard') ?>',
                            type: 'GET',
                            data: {
                                _csrf: csrfToken
                            },
                            success: function (data) {
                                $("#collapseCart").empty();

                                $("#collapseCart").append(data);
                            }
                        });

                        $.ajax({
                            url: '<?php echo Yii::$app->urlManager->createAbsoluteUrl('/site/getcardpage') ?>',
                            type: 'GET',
                            data: {
                                _csrf: csrfToken
                            },
                            success: function (data) {
                                $("#get_card2").empty();

                                $("#get_card2").append(data);
                            }
                        });

                    }
                });
            }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM