【问题标题】:How do I call the function while onchange in jquery?jquery中的onchange时如何调用该函数?
【发布时间】:2018-04-02 08:56:59
【问题描述】:

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

我正在努力在下面的行中调用 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>

【问题讨论】:

  • 你试过 this.getOptions 吗?
  • 我已经尝试过 onchange="this.getOptions()"
  • 但它显示为如下错误(索引):1 Uncaught TypeError:this.getOptions is not a function at HTMLSelectElement.onchange ((index):1)

标签: javascript php jquery templates magento


【解决方案1】:

您的代码有语法错误

你需要有“”

    onchange = "abc.getOptions()"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多