0

I'm using October CMS and in the framework I can make an AJAX call using the following HTML element:

<a href="#" class="sbutton" data-request="onSavedeal" 
     data-request-data="deal_ID:'14779255',type:'local',active:'0'">
    <i class="material-icons pink-text listfavorite">favorite</i>                      
</a>

Whenever this link is clicked on a favorite button fires off an update to a controller "onSavedeal". Updating the database works fine on the first click. However, after updating, the value for the "data-request-data" attribute isn't updated so the button does not work for subsequent clicks.

I need to make the link change so that "active:'0'" becomes "active:'1'". The resulting full element would be

<a href="#" class="sbutton" data-request="onSavedeal" 
     data-request-data="deal_ID:'14779255',type:'local',active:'1'">
    <i class="material-icons pink-text listfavorite">favorite</i>                      
</a>

In the framework I can add another attribute called "data-request-success" which executes a javascript function (or code) up successful completion of the AJAX call. How can I make a function called "updateactive()" which would toggle the active value between 0 and 1. The final element should look like:

<a href="#" class="sbutton" data-request="onSavedeal" 
     data-request-data="deal_ID:'14779255',type:'local',active:'0'"
     data-reuqest-success="updateactive();">
    <i class="material-icons pink-text listfavorite">favorite</i>                      
</a>
4

2 回答 2

1

如果您可以通过具有 valuea的属性安全地识别此元素,则可以编写如下函数:data-requestonSavedealupdateactive

function updateactive() {
    var newAttrValue = "deal_ID:'14779255',type:'local',active:'1'";
    $('[data-request="onSavedeal"]').attr('data-request-data', newAttrValue);
 }

不要忘记更正data-reuqest-successdata-request-success

更新 如果a页面上有很多元素,您可以重复使用相同的功能,如下所示。检查演示-小提琴

function updateactive() {
    var clickedA = event.currentTarget,
        newAttrValue = $(clickedA).attr('data-request-data').replace("active:'0'", "active:'1'");

    $(clickedA).attr('data-request-data', newAttrValue);
}
于 2016-02-28T04:53:37.403 回答
0

以这种方式更改您的锚标记

<a href="#" class="sbutton" data-request="onSavedeal" 
 data-request-data="deal_ID:'14779255',type:'local',active:'0'"
 data-reuqest-success="updateactive(this);">
<i class="material-icons pink-text listfavorite">favorite</i>                      

注意变化updateactive(this);

然后在您的 jquery 中,您可以通过这种方式定义函数。

 function updateactive(element) {        
    $(element).attr('data-request-data', $(element).attr('data-request-data').replace("active:'0'", "active:'1'"));
}
于 2016-02-28T05:12:04.353 回答