gpt4 book ai didi

javascript - 如何在 html 表体中使用 jQuery 或 javascript?

转载 作者:行者123 更新时间:2023-11-30 00:12:03 25 4
gpt4 key购买 nike

我有一个要求如下。我正在检查已登录的用户。根据用户 ID,我希望启用或禁用保存按钮?

如何使用 jQuery 或 javascript 检查它?我的代码如下。

// Function for getting userID based on name that has been existing in localstorage item.

function getUserIDbyName() {
var username = localStorage.getItem("activeUser");
var info = {
uname: username
};
jQuery.ajax({
type: 'GET',
url: '@Url.Action("GetUserIDByName", "MyWorkFlow")',
data: info,
success: function (data) {
if (data != null) {
document.getElementById("userID").value = data;
}
}
});
}



My Html code:

<td>
<input type="hidden" id="userID" />
</td>

<td>
@if(item.UID==1){ // I had hardcoded the value = 1 as of now. I want to check with value in $("userID").val(); which means I want to check like this if(item.UID==$("userID").val())
<button class="btn btn-primary btn-sm" value="@item.ID" id="btnSubmit" onclick="fn(this)"><span class="glyphicon glyphicon-save"></span>Save</button>
}
else{
<button disabled="disabled" class="btn btn-primary btn-sm" value="@item.ID" id="btnSubmit" onclick="fn(this)"><span class="glyphicon glyphicon-save"></span>Save</button>
}
</td>

请帮帮我

最佳答案

您可以将按钮设置为默认禁用。像这样:

<button disabled="disabled" class="btn btn-primary btn-sm" value="@item.ID" id="btnSubmit_@item.ID" onclick="fn(this)"><span class="glyphicon glyphicon-save"></span>Save</button>

然后在 success 函数中比较项目 id。如果 UID 等于用户 ID,则删除禁用的属性。并在进行比较之前确保两个值具有相同的数据类型:

var UID = '@item.UID';
jQuery.ajax({
type: 'GET',
url: '@Url.Action("GetUserIDByName", "MyWorkFlow")',
data: info,
success: function (data) {
if (data != null) {
document.getElementById("userID").value = data;
if(UID == data){//make sure both are having same datatype
$('.btn-sm[value='+ data +']').removeAttr("disabled");
}
}
}
});

关于javascript - 如何在 html 表体中使用 jQuery 或 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36054665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com