Jquery .val()通过按下按钮来更改变量值

时间:2014-12-19 10:14:39

标签: javascript jquery html

我想通过触摸按钮在Jquery中传递限制值。我几乎找到了解决方案,但不太明白。如何将值设置为"限制:"默认值并按下按钮进行更改。

HTML     

<button id="pagelimit default">1</button>
<button id="pagelimit">2</button>
<button id="pagelimit">3</button>

</div>

JS

function displayVals() {
var pagelim = $( "#pagelimit" ).val();
};

$('#product-grid').mixItUp({
pagination: {
    limit: $pagelim // insert button value
}
});

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

元素的ID必须是唯一的...看起来您想要使用单击的按钮文本更新limit选项,所以

<button class="pagelimit default">1</button>
<button class="pagelimit">2</button>
<button class="pagelimit">3</button>

然后

//this method is not used in the below code as we don't know which button was clicked here... if you share how the `displayVals` method is called then we can try to make this work
function displayVals() {
    var pagelim = $("#pagelimit").val();
};

jQuery(function ($) {
    $('.pagelimit').click(function () {
        //update the limit option
        $('#product-grid').mixItUp('setOptions', {
            limit: +$(this).text()
        });
    });

    //initialize the plugin
    $('#product-grid').mixItUp({
        pagination: {
            limit: 1
        }
    });
})