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

时间:2018-04-02 08:57:00

标签: javascript php jquery templates magento

我创建了一个模板来显示后端的下拉字段。 第一个字段包含属性,第二个字段包含选项。

我很难在下面的行中调用getOptions函数。 它显示错误是ABC未定义。请为我提供在创建选择字段时调用函数的解决方案

我正在使用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 个答案:

答案 0 :(得分:0)

您的代码有语法错误

你需要“”

    onchange = "abc.getOptions()"
相关问题