Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在html表体中使用jQuery或javascript?_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

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

如何在html表体中使用jQuery或javascript?,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我有如下要求。我正在检查已登录的用户。根据用户ID,我希望启用或禁用保存按钮 如何使用jQuery或javascript检查它?我的代码如下 // Function for getting userID based on name that has been existing in localstorage item. function getUserIDbyName() { var username = localStorage.getItem("activeUser");

我有如下要求。我正在检查已登录的用户。根据用户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>
//用于根据localstorage项中已存在的名称获取用户标识的函数。
函数getUserIDbyName(){
var username=localStorage.getItem(“activeUser”);
变量信息={
uname:username
};
jQuery.ajax({
键入:“GET”,
url:'@url.Action(“GetUserIDyname”,“MyWorkFlow”),
数据:信息,
成功:功能(数据){
如果(数据!=null){
document.getElementById(“userID”).value=数据;
}
}
});
}
我的Html代码:
@if(item.UID==1){//我现在已经硬编码了值=1。我想用$(“userID”).val()中的值进行检查,这意味着如果(item.UID=$(“userID”).val()我想这样检查
拯救
}
否则{
拯救
}

请帮助我

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

<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>

旁注:只需确保您在服务器端altso上检查此项,否则每个人都可以使用您的save-button@andreasnico:的确如此。用户可以轻松地从控制台中删除禁用的属性。只有分配给已登录用户的活动才能单击“保存”。休息将被禁用。我需要这个需求的逻辑,我是在客户端自己做的,我在遍历包含列表的数据。如果我按照你建议的方式去做,那么所有的按钮都将被禁用。@PSriharsha:那么你应该根据它们的值来找到按钮(选中上面的编辑)。当您获得用户id时。然后启用它。在这种情况下,ID也应该是唯一的。并非所有按钮都应具有
btnSubmit
作为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");
                } 
            }
        }
    });