使用jquery根据按钮值调用不同的URL
问题描述:
我有一个按钮,调用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
答
您可以生成的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
用户如何激活或取消激活记录? – 2013-02-13 03:00:49