简体   繁体   English

单击按钮更改数据值时

[英]When click on button to change data-value

I have a hyperlink which have to change direction of sorting. 我有一个超链接,必须更改排序的方向。

<a href="#" id="sort-newest" th:attr="data-value=${sortdir}">Newest</a>

When click on the link, I want to change data-value whithout refresh page. 当单击链接时,我想在没有刷新页面的情况下更改数据值。

$('#sort-newest').click(function(event){
    var url = [[${url_current}]];
    var sortby = 'isNew';
    var sortdir = $(this).attr('data-value');

    if (sortdir == 'ASC') {
        sortdir = 'DESC';
    } else if(sortdir == 'DESC') {
        sortdir == 'ASC';
    }

    var data = {
        sortby: sortby,
        sortdir: sortdir
    };

    $('#productList').load(url, data);
});

This is not working and I can not understand why. 这是行不通的,我不明白为什么。 Can you help me please ? 你能帮我吗 ?

Guess you are using jQuery 猜猜您正在使用jQuery

 var sortdir = $(this).attr('data-value');

for the above code to work you must have 为了使以上代码正常工作,您必须

data-value="${sortdir}" not th:attr="data-value=${sortdir}" data-value="${sortdir}"而不是th:attr="data-value=${sortdir}"

<input type="text" data-value="10">

如果您想使用jQuery更改数据值

$("element").data("value") = "25";

You have invalid attribute. 您的属性无效。

Change th:attr="data-value=${sortdir}" to data-value="${sortdir}" . th:attr="data-value=${sortdir}"更改为data-value="${sortdir}"

<a href="#" id="sort-newest" data-value="${sortdir}">Newest</a>

Use the .attr() jQuery function to set/update the attribute: 使用.attr() jQuery函数设置/更新属性:

$('#sort-newest').attr('data-value', 'new value');

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

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