【问题标题】:Input field read-only when it has value from database otherwise when it's null it should be editable输入字段在其具有来自数据库的值时为只读,否则当它为空时它应该是可编辑的
【发布时间】:2021-11-22 18:12:04
【问题描述】:

我是这个社区的新手,非常感谢您的帮助。 这是我的代码行:

<input type="text" class="form-control" id="fines" name="fines" placeholder = "Amount Penalty" required>

我的 Javascript 代码:


$(document).ready (function(){



  if($(this).val()==""){
    $('#fines').attr('readOnly','false');
  }
  

});

</script>```




The thing that I want to achieve is that when clicking the field with no value it must be editable and if it has a value from the database it will remain a read-only field. 

Your help is highly appreciated thank you!!



  

【问题讨论】:

  • 问题需要更多细节。您是否在加载现有数据?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: javascript php html css


【解决方案1】:

您可以这样做,因为您使用 php 变量进行文本输入。

方法一:-

<?php
    if( $row['fines'] == '' || $row['fines'] == NULL){
  echo '<input type="text" class="form-control" value="" id="fines" name="fines" placeholder = "Amount Penalty" required>';
    }
    else{
  echo '<input type="text" class="form-control" value="'.$row['fines'].'" id="fines" name="fines" placeholder = "Amount Penalty" readonly>';
    }
?>

方法二:-

<?php
    if( $row['fines'] == '' || $row['fines'] == NULL){
      echo '<input type="text" class="form-control" value="" id="fines" name="fines" placeholder = "Amount Penalty" required>';
    }
    else{
      echo $row['fines'];//Show it as plain text, ez
    }
?>

注意:-当您使用Method 1:- 时,仍然有可能使输入文本可写,只需检查它并更改值对用户来说就足够容易了,我认为Method 2:-好多了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2019-01-22
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多