2013-02-13 49 views
0

我有一個按鈕,調用CFM頁面,它需要傳遞一個值,取決於用戶是否正在激活或停用數據庫中的記錄。我正在努力如何將活動/不活動值傳遞給jQuery。以下是我正在使用的代碼,它只能單向運行,即從活動狀態變爲非活動狀態。使用jquery根據按鈕值調用不同的URL

<script> 
$(document).ready(function() { 

    $("#IsActive_1").click(function() { 

    //cache the status div 
     var status = $("#status"); 

     //let the user know the record is being updated 
     status.html("<i>updating status...</i>"); 

    $("#IsActive_1").html('activate'); 

     //the status variable needs to be dynamic 
     $.get("updateStatus.cfm?status=inactive", {}, function(res,code) { 
     //show the result in plain HTML 
     status.html(res); 
    }); 

    }); 

}); 
</script> 


<button id="IsActive_1"><cfif IsActive>deactivate<cfelse>activate</cfif></button><div id="status"></div> 

最後,我想改變按鈕上顯示激活文本/停用基於適當的條件(點擊取消,反之亦然後激活)。

TIA

+0

用戶如何激活或取消激活記錄? – 2013-02-13 03:00:49

回答

0

您可以生成的attr添加到該按鈕,並在此基礎上,送出可變。

<button id="IsActive_1" checked="checked">Deactivate</button> 


    $("#IsActive_1").click(function() { 
      var x = $(this); 
      var isActive = x.attr('checked') != undefined; 
      if (isActive) 
      x.removeAttr('checked'); 
      else 
      x.attr('checked', 'checked'); 
     //cache the status div 
      var status = $("#status"); 

      //let the user know the record is being updated 
      status.html("<i>updating status...</i>"); 

      x.html(isActive ? 'Activate' : 'Deactivate'); 

      //the status variable needs to be dynamic 
      $.get("updateStatus.cfm", { status : isActive ? 'inactive' : 'active' }, function(res,code) { 
      //show the result in plain HTML 
      status.html(res); 
     }); 

     }); 
+0

完美!謝謝! – saoco 2013-02-14 21:34:52

相關問題