简体   繁体   English

从更新的范围滑块中获取价值?

[英]Get value from updated range slider?

I've got a range slider for jQuery Mobile. 我有一个用于jQuery Mobile的范围滑块。 I want to combine the result from an autocomplete and the two slider values, but I only seem to get the predefined HTML value from the slider, instead of the one I'm currently choosing from sliding it. 我想将自动完成的结果和两个滑块的值结合起来,但是我似乎只是从滑块中获得了预定义的HTML值,而不是我目前正在选择的HTML值。

Here's the JS: 这是JS:

$(function() {
    function log(message) {
        $("<div/>").text(message).prependTo("#log");
        $("#log").attr("scrollTop", 0);
    }

    $.ajax({
        url: "drycker.xml",
        dataType: "xml",
        success: function(xmlResponse) {
            var data = $("artikel", xmlResponse).map(function() {
                return {
                    value: $("Namn", this).text(),
                    id: $("Artikelid", this).text(),
                    price: $("Prisinklmoms", this).text(),
                    interval: $("#range-1a").val()
                };
            }).get();
            $("#birds").autocomplete({
                source: data,
                minLength: 0,
                select: function(event, ui) {
                    log(ui.item ?
                        "Vald produkt: " + ui.item.value + ", artikel-ID: " + ui.item.id + ", pris: " + ui.item.price + ", prisintervall vald:" + ui.item.interval:

                        "Ingen vald produkt, sökningen var " + this.value);
                }
            });
        }
    });
});

and the needed HTML: 以及所需的HTML:

    <div data-role="rangeslider">
        <label for="range-1a">Prisintervall:</label>
        <input name="range-1a" id="range-1a" min="0" max="500" value="20" type="range">
        <label for="range-1b">Prisintervall:</label>
        <input name="range-1b" id="range-1b" min="0" max="500" value="200" type="range">
    </div>

So, the problem is that 所以,问题是

interval: $("#range-1a").val()
only gives me the value 20 because that's what #range-1a is set to from the beginning. 只给我值20,因为这是#range-1a从一开始就设置为的值。 I'm never getting the new value. 我永远都不会获得新的价值。 Any ideas? 有任何想法吗?

What you're doing looks right to me. 你在做什么对我来说很对。 Try bringing up a web inspector / developer tool bar and running just 尝试调出Web检查器/开发人员工具栏并仅运行

$("#range-1a").val(); $(“#range-1a”)。val();

to verify that jQuery is correctly returning the results you are after. 验证jQuery是否正确返回了您想要的结果。 Confirm that $('#yourID').val() is working outside of the AJAX request. 确认$('#yourID')。val()在AJAX请求之外正常工作。

You need to add a SUBMIT or GO button and the related onClick handler. 您需要添加一个SUBMIT或GO按钮以及相关的onClick处理程序。

Here is the problem with the posted code: it only gets executed once, at page load. 这是发布的代码的问题:页面加载时,它只会执行一次。 That includes the ajax request. 这包括ajax请求。 It is executed once, at page load. 它在页面加载时执行一次。

That's why the server response corresponds to the initial settings. 这就是服务器响应与初始设置相对应的原因。 The web browser is never told to fetch the information corresponding to any new or updated request. 永远不会告诉Web浏览器获取与任何新的或更新的请求相对应的信息。

You need event handlers that detect changes to the slider, and resubmit the ajax and response handler. 您需要事件处理程序来检测滑块的更改,然后重新提交ajax和响应处理程序。 So it appears that the standard jQuery $('selector').on('change', callbackFunction) (docs: http://api.jquery.com/on/ ) works here. 因此,似乎标准jQuery $('selector').on('change', callbackFunction) 。on $('selector').on('change', callbackFunction) (docs: http : //api.jquery.com/on/ )在这里起作用。

Here's a debugging prototype. 这是调试原型。 You'll see it now prints the left slider setting on the console log whenever you move either slider. 您现在将看到,无论何时移动任一滑块,它都会在控制台日志上打印左滑块设置。 The ajax was disabled but saved in a function. Ajax已禁用,但已保存在函数中。 You could set the change handling function to call the ajax function, but it fires quite frequently as the slider is dragged and so you would need to be debounce it. 您可以将更改处理函数设置为调用ajax函数,但是在拖动滑块时它会非常频繁地触发,因此您需要对其进行去抖动处理。 It is easier and might be a better user experience to have them push a button (that you'll need to add) to send a request. 让他们按下一个按钮(您需要添加)来发送请求会更容易,并且可能会更好,从而使用户体验更好。 In that case you'll need to bind the button's onClick function to the function that calls the ajax and updates the page. 在这种情况下,您需要将按钮的onClick函数绑定到调用ajax并更新页面的函数。 Also you'll want to remove the window.setTimeout and reporter() function after you are satisfied you know how it works. 另外,您满意后就想删除window.setTimeout和Reporter()函数。

Here's the code for http://jsfiddle.net/FnrcR/1/ 这是http://jsfiddle.net/FnrcR/1/的代码

function reporter(){
    console.log("range-1a");
    console.log($("#range-1a").val());
//    window.setTimeout(reporter, 5000);
}

function doAjaxRequest(){
    $.ajax({
            url: "http://erikblomqvist.com/sandbox/dryck/drycker.xml",
            dataType: "xml",
            success: function(xmlResponse) {
                var data = $("artikel", xmlResponse).map(function() {
                    return {
                        value: $("Namn", this).text(),
                        id: $("Artikelid", this).text(),
                        price: $("Prisinklmoms", this).text(),
                        interval: $("#range-1a").val()
                    };
                }).get();
                $("#birds").autocomplete({
                    source: data,
                    minLength: 0,
                    select: function(event, ui) {
                        log(ui.item ?
                            "Vald produkt: " + ui.item.value + ", artikel-ID: " + ui.item.id + ", pris: " + ui.item.price + ", prisintervall vald:" + ui.item.interval:

                            "Ingen vald produkt, sökningen var " + this.value);
                    }
                });
            }
        });
}    

$(function() {
    function log(message) {
        $("<div/>").text(message).prependTo("#log");
        $("#log").attr("scrollTop", 0);
    }
     // window.setTimeout(reporter, 1000); 
    $("#myRange").on("change",  reporter);
});

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

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