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

时间:2016-03-17 08:02:54

标签: javascript jquery asp.net-mvc

我有如下要求。我正在检查已登录的用户。根据userID,我想启用还是禁用保存按钮?

如何使用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>

请帮帮我

1 个答案:

答案 0 :(得分:1)

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

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

然后比较成功函数中的项目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");
                } 
            }
        }
    });