Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery触发原型更改事件_Javascript_Jquery_Ajax_Prototypejs - Fatal编程技术网

Javascript 使用jQuery触发原型更改事件

Javascript 使用jQuery触发原型更改事件,javascript,jquery,ajax,prototypejs,Javascript,Jquery,Ajax,Prototypejs,我在原型方面没有太多的经验,我一直在使用jQuery使事情正常工作。今天,我添加了一个用于会话处理的基于jQuery的脚本。我遇到的问题是,尽管我今天在功能方面已经取得了很大的进步,但我似乎无法通过jQuery触发更改事件 我目前正在使用以下代码,但它工作不正常(您可以在网站上测试它..一旦您使用鼠标更改年份,AJAX就会触发,并且可以选择Make) 下面的代码是控制这一点的原型脚本,我需要触发更改事件,如果可能的话,我希望通过jQuery来完成 var amFinder = new Class

我在原型方面没有太多的经验,我一直在使用jQuery使事情正常工作。今天,我添加了一个用于会话处理的基于jQuery的脚本。我遇到的问题是,尽管我今天在功能方面已经取得了很大的进步,但我似乎无法通过jQuery触发更改事件

我目前正在使用以下代码,但它工作不正常(您可以在网站上测试它..一旦您使用鼠标更改年份,AJAX就会触发,并且可以选择Make)

下面的代码是控制这一点的原型脚本,我需要触发更改事件,如果可能的话,我希望通过jQuery来完成

var amFinder = new Class.create();

amFinder.prototype = {
    initialize: function(containerId, ajaxUrl, loadingText, isNeedLast, autoSubmit)
    {
        this.containerId = containerId;
        this.ajaxUrl     = ajaxUrl;
        this.autoSubmit  = Number(autoSubmit);
        this.loadingText = loadingText;
        this.isNeedLast  = Number(isNeedLast);
        this.selects     = new Array();

        //possible bug if select order in the HTML will be different
        $$('#' + this.containerId + ' select').each(function(select){
            select.observe('change', this.onChange.bindAsEventListener(this));
            this.selects[this.selects.length] = select;
        }.bind(this));
    },

    onChange: function(event)
    {
        var select     = Event.element(event);
        var parentId   = select.value;
        var dropdownId = 0;

        /* should load next element's options only if current is not the last one */
        for (var i = 0; i < this.selects.length; i++){
            if (this.selects[i].id == select.id && i != this.selects.length-1){
                var selectToReload = this.selects[i + 1];
                if (selectToReload){
                    dropdownId = selectToReload.id.substr(selectToReload.id.search('--') + 2);
                }
                break;
            }
        }

        this.clearAllBelow(select);

        if (0 != parentId && dropdownId){
            var postData = 'dropdown_id=' + dropdownId + '&parent_id=' + parentId;
            new Ajax.Request(this.ajaxUrl, {
                method: 'post',
                postBody : postData,
                evalJSON : 'force',

                onLoading: function(){
                    this.showLoading(selectToReload);
                }.bind(this),

                onSuccess: function(transport) {
                    if (transport.responseJSON){
                        this.clearSelectOptions(selectToReload);
                        var itemsFound = false;
                        transport.responseJSON.each(function(item){
                            itemsFound = true;
                            var option = document.createElement('option');
                            option.value = item.value;
                            option.text  = item.label;
                            option.label = item.label;
                            $(selectToReload).appendChild(option);
                        });

                        if (itemsFound){
                            $(selectToReload).disabled = false;
                        }
                    }
                }.bind(this)
            });
        }
    },

    isLast: function(select)
    {
        return (this.selects[this.selects.length-1].id == select.id);    
    },

    isFirst: function(select)
    {
        return (this.selects[0].id == select.id);         
    },

    clearSelectOptions: function(select)
    {
        $(select).descendants().each(function(option){
            option.remove();
        });
    },

    clearAllBelow: function(select)
    {
        var startClearing = false;
        for (var i = 0; i < this.selects.length; i++){
            if (startClearing){
                this.clearSelectOptions(this.selects[i]);
                $(this.selects[i]).disabled = true;
            }
            if (this.selects[i].id == select.id){
                startClearing = true;
            }
        }
        var type = (((this.isLast(select) && !this.isNeedLast) && select.value > 0) || ((this.isNeedLast) && ((select.value > 0) || (!this.isFirst(select))))) ? 'block' : 'none';

        if ('block' == type && this.autoSubmit && this.isLast(select))
        {
            $$('#' + this.containerId + ' .amfinder-buttons button')[0].form.submit();
        } else {
            $$('#' + this.containerId + ' .amfinder-buttons')[0].style.display = type;            
        }


    },

    showLoading: function(selectToReload)
    {
        var option = document.createElement('option');
        option.value = 0;
        option.text  = this.loadingText;
        option.label = this.loadingText;
        $(selectToReload).appendChild(option);        
    },
};
var amFinder=newclass.create();
amFinder.prototype={
初始化:函数(containerId、ajaxUrl、loadingText、isNeedLast、autoSubmit)
{
this.containerId=containerId;
this.ajaxUrl=ajaxUrl;
this.autoSubmit=编号(autoSubmit);
this.loadingText=loadingText;
this.isNeedLast=编号(isNeedLast);
this.selects=新数组();
//如果HTML中的选择顺序不同,则可能出现错误
$$('#'+this.containerId+'select')。每个(函数(选择){
选择.observe('change',this.onChange.bindAsEventListener(this));
this.selects[this.selects.length]=select;
}.约束(这个);
},
onChange:函数(事件)
{
var select=Event.element(事件);
var parentId=select.value;
var-dropdownId=0;
/*仅当current不是最后一个元素时,才应加载下一个元素的选项*/
for(var i=0;i0)| |((this.isNeedLast)和&((select.value>0)| |(!this.isFirst(select);))?“块”:“无”;
if('block'==type&&this.autoSubmit&&this.isLast(选择))
{
$$(“#”+this.containerId+”.amfinder按钮“)[0].form.submit();
}否则{
$$('#'+this.containerId+'.amfinder按钮')[0].style.display=type;
}
},
显示加载:功能(选择加载)
{
var option=document.createElement('option');
option.value=0;
option.text=this.loadingText;
option.label=this.loadingText;
$(选择加载)。追加子项(选项);
},
};

我也有同样的问题。这是解决办法。当您创建:amfinder horizontal时,html是这样的

<div class="amfinder-horizontal" id="amfinder_5333ffb212b09Container">
   ...
</div>
<div class="amfinder-horizontal" id="amfinder_5333ffb212b09Container">
   ...
</div>
<?php $finderId = 'amfinder_' . uniqid(); ?>
...
<script type="text/javascript">
var <?php echo $finderId ?>  = new amFinder(
    '<?php echo $finderId ?>Container', 
    '<?php echo $this->getAjaxUrl() ?>', 
    '<?php echo $this->__('Loading...')?>',
    '<?php echo Mage::getStoreConfig('amfinder/general/partial_search')?>',
    <?php echo intval(Mage::getStoreConfig('amfinder/general/auto_submit')) ?>
);
</script>
// Find the name for amfinder object.
var objName = jQuery("div.amfinder-horizontal").attr('id').replace('Container','');

// set Value to that select - copied from your code
jQuery("div.amfinder-horizontal td:nth-child(1) select option").each(function() { this.selected = (this.text == "2003"); });

// call the change event of that object
var targetObj = {
    target : jQuery("div.amfinder-horizontal td:nth-child(1) select")[0]
};
window[objName].onChange(targetObj);