简体   繁体   English

有条件地设置可编辑或只读文本字段

[英]Conditionally set editable or readonly text field

How to conditionally enable or disable to editable field (edit/read only)? 如何有条件地启用或禁用可编辑字段(编辑/只读)? I used session role from database to make condition on the text field. 我使用数据库中的会话角色来为文本字段创建条件。 However, I don't know how to proceed it... 但是,我不知道如何进行......

<?php
include('session.php');
?>

<tr class="row1">
<td width="30%"><div align="left">Customer name1</div></td>
<td width="100%">
    <div align="left">
        <input type="text" name="CMCNA1" id="CMCNA1" style="width:100%;" pattern="[A-Za-z]+" title="Please insert alphabetical only" maxlength="35"/>
    </div>
</td>

Try this -->> 试试这个 - >>

<script type="text/javascript" >
function makerCheckerField()
{
var role= <?php echo $_SESSION['login_user_role']; ?>;
var maker="Maker";
if (role === maker) //  equal value and equal type
{
    $("#CMCNA2").attr("readonly", true);

} else {$("#CMCNA2").attr("readonly", false);}
}
 </script>

你可以添加attribut readonly =“true”来使输入只读/不可编辑:

<input type="text" name="CMCNA1" id="CMCNA1" style="width:100%;" pattern="[A-Za-z]+" title="Please insert alphabetical only" maxlength="35" readonly="true" />

I know it's an old post, but just in case, for those who have this needs. 我知道这是一个老帖子,但为了以防万一,对于那些有这种需求的人。 Base on the code of Narayan , maybe by doing this (with jquery): 根据Narayan的代码,也许通过这样做(使用jquery):

<script type="text/javascript" >

function makerCheckerField()
{
    var role= <?php echo $_SESSION['login_user_role']; ?>;

    var maker='Maker';

    if (role === maker){
        $('#CMCNA1').attr('readonly', 'readonly');
    } else {
        $('#CMCNA1').removeAttr('readonly');
    }
} 
</script>

If I am not wrong the readonly property works like this : 如果我没看错的readonly属性的工作原理是这样

readonly = "readonly" or "" (empty string) or empty Specifies that element represents a control whose value is not meant to be edited. readonly =“readonly”或“”(空字符串)或空指定该元素表示不打算编辑其值的控件。

Maybe you will have to manage correctly the differents states of roles (what happens if you came back to the default state etc...). 也许你必须正确管理角色的不同状态(如果你回到默认状态等会发生什么......)。

You can also play with 你也可以玩

.setAttribute, .getAttribute and .removeAttribute

I don't know if this is what you was looking for. 我不知道这是不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM