简体   繁体   English

如何将输入字段设置为只读或输入字段

[英]how to set input field as readonly or entry field

I am having input item if data is available the field should be readonly else required field.如果数据可用,我有输入项,该字段应为只读,否则为必填字段。

  <input type="text" name="login_password" 
  id="login_password" 
 placeholder="Desgination" value="<?php echo 
   $row["designation"] ?>" Readonly required>

You can try this out, if there is some value in the field it will echo readonly else it will be blank field with required你可以试试这个,如果字段中有一些值,它将回显readonly ,否则它将是空白字段, required

<input type="text" name="login_password"   id="login_password"  placeholder="Desgination" value="<?php echo  $row["designation"]; ?>"  <?php if(trim($row["designation"]) !="") echo "Readonly"; else echo "required"; ?> >

My 2 cents, Without if inside the html code, a bad habit imho我的 2 美分,如果没有在 html 代码中,恕我直言,这是一个坏习惯

$readonly = isset($row["designation"]) ? 'required' : '';

<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo $row["designation"] ?>" <?php echo $readonly; ?> required>

Try This尝试这个

<?php 
    if(!empty($row["designation"])){
       $condition = "readonly";
    else
      $condition = "required"; 
?>
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo 
$row["designation"] ?>" <?php echo $condition; ?>>

put before end of input readonly like <input /*somthing*/ readonly><input /*somthing*/ readonly>这样放在 input 的末尾 readonly

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

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