简体   繁体   English

jQuery Ajax选择表单加载网址不起作用

[英]jQuery ajax select form load url not working

I am a designer who is trying to learn ajax/js through the development of my own site and having problems loading an external url through a form select function. 我是一位设计师,我试图通过自己网站的开发来学习ajax / js,但在通过表单选择功能加载外部URL时遇到问题。

The problem. 问题。 I have a set of icons on the left hand side which load url using ajax and pass the variable to a form select on the right hand side of the navigation. 我在左侧有一组图标,这些图标使用ajax加载url并将变量传递给导航右侧的表单选择。 This works fine. 这很好。 The form select function does not load the url when a value is selected from the dropdown. 从下拉列表中选择值时,表单选择功能不会加载url。

Link to test http://www.diztinct.co.uk/clients/Diztinct/home.html (functionality directly under the yellow banner) 链接到测试http://www.diztinct.co.uk/clients/Diztinct/home.html (功能直接在黄色横幅下)

Section of html HTML部分

      <div class="panel-holder">
                <ul id="quick-filter" class="rubrics">
                    <li><a data-filter="web-design" data-filter-url="inc/web-design.html" href="#"><span>Web Design</span></a></li>
                    <li><a data-filter="responsive-design" data-filter-url="inc/responsive-web-design.html" class="respsonive-design tooltip" href="#"><span>Respsonive</span></a></li>
                    <li><a data-filter="user-interface-design" data-filter-url="inc/user-interface-design.html" class="ui-design tooltip" href="#"><span>UI Design</span></a></li>
                    <li><a data-filter="brand-identity-design" data-filter-url="inc/brand-identity-design.html" class="brand tooltip" href="#"><span>Brand</span></a></li>
                    <li><a data-filter="creative-production" data-filter-url="inc/creative-production-design.html" class="creative tooltip" href="#"><span>Creative</span></a></li>
                </ul>
                <form id="filter-form" class="request-form" action"#">
                    <fieldset>
                        <select id="filter-select" >
                            <option value="all" data-filter-url="inc/list.html" title="images/ico1.png">Recent Updates</option>
                            <option value="brand-identity-design" data-filter="brand-identity-design" data-filter-url="inc/brand-identity-design.html" title="images/ico2.png" href="#" >Brand Identity</option>
                            <option value="web-design" data-filter="web-design" data-filter-url="inc/web-design.html" title="images/ico3.png">Web Design</option>
                            <option value="responsive-design" data-filter="responsive-design" data-filter-url="inc/responsive-web-design.html" title="images/ico4.png" >Responsive Design</option>
                            <option value="user-interface-design" data-filter="user-interface-design" data-filter-url="inc/user-interface-design.html" title="images/ico5.png">User Interface Design</option>
                            <option value="creative-production" data-filter="creative-production" data-filter-url="inc/creative-production-design.html" title="images/ico6.png">Creative Production</option>
                        </select>
                    </fieldset>
                </form>
      </div>

Js. JS。 I suspect I've missed/messed something in the filterSelect.change(function()? 我怀疑我在filterSelect.change(function()中错过了/弄乱了什么吗?

//initFilter
function initFilter() {
var filterForm = jQuery('#filter-form');
var filterSelect = jQuery('#filter-select');
var filterIcons = jQuery('#quick-filter a');
var listItems = jQuery('#item-list');
var ajaxRequest;

filterIcons.bind('click', function(e) {
    e.preventDefault();
    var value = this.getAttribute('data-filter');
    filterBox(value, this.getAttribute('data-filter-url'));
    filterSelect.val(value);
    if (filterSelect.length && filterSelect[0].jcf) {
        filterSelect[0].jcf.refreshState();
    }
});

filterSelect.change(function() {
    filterBox(this.value, this.getAttribute('data-filter-url'));
});

function filterBox(value, url) {
    if (ajaxRequest) {
        ajaxRequest.abort();
    }
    listItems.addClass('loading');
    ajaxRequest = jQuery.ajax({
        url: url,
        data: 'ajax=true&' + 'filter=' + value,
        dataType: 'text',
        type: 'get',
        success: function(data) {
            var newContent = jQuery('<div>', {
                html: data
            });

            listItems.empty();
            listItems.removeClass('loading');
            listItems.append(newContent.children());
            listItems.sameHeight({
                elements: '>div',
                skipClass: 'noheight',
                multiLine: true,
                flexible: true
            });
        },
        error: function() {
            alert('AJAX Error!');
        }
    });
}

} }

Any help/advice or links to tutorials for best practice is very much appreciated. 非常感谢任何帮助/建议或指向最佳实践的教程的链接。

this inside the change() event handler will refer to your <select> rather than the selected option. this里面change()事件处理程序将参考您<select>而不是选择的选项。 Try using: 尝试使用:

var opt = $(this).children(':selected');

// The following two lines are for debugging only:
alert(opt.val());
alert(opt.attr('data-filter-url'));

filterBox(opt.val(), opt.attr('data-filter-url'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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