dpv46227 2018-04-02 08:56
浏览 98

如何在jquery中使用onchange时调用该函数?

I have created a template to display drop-down fields in the backend. The first field contains Attributes and the second field contains options.

I am struggling to call the getOptions function in below line. It shows error is ABC is not defined. Please provide me a solution to call a function while creating the select field

I'm using Magento 2.2.1.

var html = .....onchange = abc.getOptions()>

test.phtml

<div class="fieldset-wrapper customer-information">
    <div class="fieldset-wrapper-title">
        <span class="title">
            <?= $block->escapeHtml(__('Attribute and Options')) ?>
        </span>
    </div>
    <input id="btnAdd" type="button" value="Add" />
    <br />
    <br />
    <div id="TextBoxContainer">
    </div>
    <br />
</div>
<script>
    require([
        'jquery',
        "jquery/ui",
        'mage/multiselect',
        "mage/mage"

    ], function ($) {
        var abc = {
            itemsCount: 0,
            template: function (index) {
                alert("dsfsdfbd");
                var html = '<td><input type="hidden" name="filters[' + index + '][filter_id]"  value="" id="filters_' + index + '_filter_id"/>' + '<select class="form-control" id = "filters_' + index + '_attribute_code" name="filters[' + index + '][attribute_code]" onchange = abc.getOptions()>' + '<option value="">-- Please select --</option>';
        <? php $attributes = $block -> getAttributes(); ?>
        <? php foreach($attributes as $attribute): ?>
        var label = '<?php echo $attribute->getFrontendLabel(); ?>';
                var attr_code = '<?php echo $attribute->getAttributeCode() ?>';
                html += '<option value= "' + attr_code + '">' + label + '</option>';
        <? php endforeach; ?>
                    html += '</select></td><td><select class="form-control-options" id = "filters_' + index + '_option_id" name="filters[' + index + '][option_id]" ><option value="">-- Please select --</option></select>' + '<input type="button" value="Remove" class="remove" /></td>';

                return html;

            },
            addItem: function () {
                alert("hello add item");
                var data = {
                    filter_id: '',
                    attribute_code: '',
                    option_id: '',
                    index: this.itemsCount++
                };
                data.filter_id = arguments[0];
                data.attribute_code = arguments[1];
                data.option_id = arguments[2];
                return abc.template(data.index);
            },
            getOptions: function (attribute) {
                alert("options");

            }
        };

        $("#btnAdd").bind("click", function () {
            alert('reterter');
            var div = $("<div />");
            div.html(abc.addItem());
            $("#TextBoxContainer").append(div);
        });
  <? php
  if ($this -> getFilters()):
    foreach($this -> getFilters() as $filter):
    ?>
        abc.addItem('<?php echo $filter->getId() ?>', '<?php echo $filter->getAttributeCode() ?>', '<?php echo $filter->getOptionId() ?>');
    <? php
        endforeach;
  endif;
  ?>
  });
</script>
  • 写回答

1条回答 默认 最新

  • dongxun2903 2018-04-02 10:40
    关注

    Your code has syntax error

    you need to have ""

        onchange = "abc.getOptions()"
    
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误