I have a form which has many fields, I need to display some of them as read only / disabled for staff level users while admin level users can save or modify all fields.

I have setup the following javascript which shows the fields as read only however it doesn't pass any of the data though to the model, $this->request->data is a blank array with this javascript on.

<?php if ($this->Session->read('Auth.User.group_id') == 3) { ?>

<script type="text/javascript">
$(function () {

$('.adminOnlyField').attr("readonly","true");
$('.checkbox.adminOnlyField').attr("onclick","return false");
$('.checkbox.adminOnlyField').attr("onkeydown","return false");
$('.adminOnlyField').removeClass('required');
$('.adminOnlyField').removeClass('date_picker');
$('.adminOnlyField').prop('disabled', 'disabled');

});
</script>

<?php } ?>

I need the staff users to be able to see the data but not change it for these fields with the class of adminOnlyField, the rest they should be able to edit / update.

Thanks

I then though maybe when the submit button is hit I could remove the readonly and disabled with jquery such as follows:

$('.clear_ready_only').click(function(e) 
{
   e.preventDefault();
   $('.adminOnlyField').removeAttr('disabled');
   $('.adminOnlyField').removeAttr('readonly');
   $('#CardModifysaleForm').submit();
});

It doesn't seem to work though?

有帮助吗?

解决方案

Disabled fields do not pass thru POST , you are doing:

$('.adminOnlyField').attr("readonly","true");

which makes it readonly, and it can pass thru POST, but at end you disable the same field again by doing:

$('.adminOnlyField').prop('disabled', 'disabled');

this line makes it disabled due to which it does not get POST'ed. So if you want to make it pass thru POST, make it readonly, and remove the following line:

$('.adminOnlyField').prop('disabled', 'disabled');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top