2016-05-14 24 views
0

此代碼是在僞jquery,但我如何使它作爲jQuery代碼?修改jquery到服務器端的鏈接true/false toggleClass

我需要此選項來打開或關閉圖標,具體取決於管理頁面的選擇,然後在其他頁面上以靜態標記狀態顯示此選擇。

因此,這兩個片段可否修改爲將第二個片段的切換操作包含到第一個片段中?

我需要它與C#一起工作,因爲後端將用C#編寫。

我淘淨修改這樣的代碼,因爲我不知道自己的jQuery,我需要獲得幫助它...:/

//first snippet 
 
$('a').click(function(evt) { 
 
    evt.preventDefault(); 
 
    var link = $(this).attr('href'); 
 
    $.ajax({ 
 
     method: post, 
 
     url: '/link-tracking', 
 
     data: { 
 
      href: link 
 
    } 
 
    }); 
 
}).done(function(url) { // pass the url back to the client after you incremented it 
 
    window.location = url; 
 
}); 
 

 
//second snippet 
 
$('.verify').click(function() { 
 
\t var id = $(this).parents('div').attr('id'); 
 
\t $(this).toggleClass('verified'); 
 
$.post('/page.html', { 
 
     'verified': $(this).hasClass('verified'), 
 
     'id': id 
 
    }, 
 
    function(data) { 
 
\t  if (data.verified) { 
 
     \t $(this).toggleClass('verified'); \t \t \t \t 
 
\t \t } 
 
     //some sort of update function here 
 
    }); 
 
});

+0

_「是否可以修改它們以將第二個代碼段中的切換操作包含到第一個代碼段中?」_第一個代碼段中應包含哪部分第二個代碼段?或者應該將第二個片段包含在第一個片段中? – guest271314

+0

@ guest27134嗯,我不知道如何一起實現它們。我希望div的切換動作發生在//第二個代碼片段中,因此如果有辦法將兩者浸入其中? – Krys

+0

使用相同的'.verify'選擇器? – guest271314

回答

1

只有單一的點擊處理中出現必須滿足的要求

$('.verify').click(function() { 
    var id = $(this).parents('div').attr('id'); 
    $(this).toggleClass('verified'); 
    $.post('/echo/json/', {json:JSON.stringify({ 
     'verified': $(this).hasClass('verified'), 
     'id': id 
     }) 
    }, 
    function(data) { 
    console.log(data) 
     if (data.verified) { 
     // console.log(data) 
     } 
     //some sort of update function here 
    }); 
}); 

的jsfiddle https://jsfiddle.net/516rx7xa/1/