【问题标题】:How I change properties of form inputs to disable = "false" after making it "true"将表单输入的属性设置为“true”后如何将其更改为禁用 =“false”
【发布时间】:2017-04-17 11:19:03
【问题描述】:

我有一个通过 Ajax 提交的表单。用户发送表单后,文本会发生变化,显示数据发送成功,然后显示表单已填写。我想显示表单,但我不希望他们重新提交表单,所以我想禁用输入以及提交按钮。我有 3 个操作,添加、更新和查看。在视图中禁用表单输入后,在添加中保持禁用状态。

<div id="userModal" class="modal fade">
 <div class="modal-dialog">
  <form method="post" id="user_form" enctype="multipart/form-data">
   <div class="modal-content">
    <div class="modal-header">
     <button type="button" class="close" data-dismiss="modal">&times;</button>
     <h4 class="modal-title">Add Report</h4>
    </div>
    <div class="modal-body">
     <div class="row">
     <div class="col-xs-2">
         <label for="ex1">Square</label>
         <input type="text" name="square" id="square" class="form-control">
     </div>
     <div class="col-xs-2">
         <label for="ex1"> Number </label>
         <input type="text" name="report_number" id="report_number" class="form-control">
     </div>
     <div class="col-xs-4">
         <label>Report Date </label>
         <input type="date" name="report_date" id="report_date" class="form-control" />
     </div >
     </div>

     </br>

     <div class="row">
      <div class="col-xs-12">
         <label>Notes </label>
         <textarea rows="15" cols="80" type="text" name="notes" id="notes" class="form-control" > </textarea>
     </div >
     </div>

     <br />
     <label>Select Image</label>
     <input type="file" name="user_image" id="user_image" />
     <span id="user_uploaded_image"></span>
    </div>
    <div class="modal-footer">
     <input type="hidden" name="report_id" id="report_id" />
     <input type="hidden" name="operation" id="operation" />
     <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
   </div>
  </form>
 </div>
</div>


<script>

 $(document).on('submit', '#user_form', function(event){
  event.preventDefault();

  var addedSquare = $('#square').val();
  var addedNumber = $('#report_number').val();
  var addedDate = $('#report_date').val();
  var extension = $('#user_image').val().split('.').pop().toLowerCase();
  if(extension != '')
  {
   if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
   {
    alert("Invalid Image File");
    $('#user_image').val('');
    return false;
   }
  } 
  if(addedSquare != '' && addedNumber != '')
  {
   $.ajax({
    url:"insert.php",
    method:'POST',
    data:new FormData(this),
    contentType:false,
    processData:false,
    success:function(data)
    {

     alert(data);
     $('#user_form')[0].reset();
     $('#userModal').modal('hide');

     dataTable.ajax.reload();
    }
   });
  }
  else
  {
   alert("Both Fields are Required");
  }
 });

 $(document).on('click', '.update', function(){
  var report_id = $(this).attr("id");
  $.ajax({
   url:"fetch_single.php",
   method:"POST",
   data:{report_id:report_id},
   dataType:"json",
   success:function(data)
   {
    $('#userModal').modal('show');
    $('#square').val(data.square).prop( "disabled", false );
    $('#report_number').val(data.report_number).prop( "disabled", false );
    $('#report_date').val(data.report_date).prop( "disabled", false );
    $('.modal-title').text("Edit Report");
    $('#report_id').val(report_id);
    $('#user_uploaded_image').html(data.user_image);
    $('#action').val("Edit");
    $('#operation').val("Edit");
   }
  })
 });

 $(document).on('click', '.view', function(){
  var report_id = $(this).attr("id");
  $.ajax({
   url:"fetch_single.php",
   method:"POST",
   data:{report_id:report_id},
   dataType:"json",
   success:function(data)
   {
    $('#userModal').modal('show');
    $('#square').val(data.square).prop( "disabled", true );
    $('#report_number').val(data.report_number).prop( "disabled", true );
    $('#report_date').val(data.report_date).prop( "disabled", true );
    $('.modal-title').text("View Report");
    $('#report_id').val(report_id);
    $('#user_uploaded_image').html(data.user_image);
    $('#action').val("View");
    $('#operation').val("View");
   }
  })
 });


</script>

如何在 add 中再次启用输入?

【问题讨论】:

  • 那么您使用.prop( "disabled", true ); 来禁用它们,.prop( "disabled", false ); 不能再次启用它们?
  • 添加时 .prop("disabled", true);到视图模式,然后输入在添加模式中保持禁用状态,我尝试添加 .prop("disabled", false);到添加模式,但它仍然被禁用
  • 该函数有效 (jsfiddle.net/sqrxgjru),如果它不在您的代码中,请尝试尝试编辑您的问题(如果没有所有 ajax 内容,请提供简化版本)。

标签: javascript ajax twitter-bootstrap


【解决方案1】:

只是 .prop( "禁用" , false );

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2011-11-23
    • 1970-01-01
    • 2011-10-07
    • 2017-07-12
    • 2014-06-20
    • 2013-06-08
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多