Login

Disable fields in oldforms admin using jQuery

Author:
schraal
Posted:
June 17, 2008
Language:
JavaScript
Version:
Not specified
Tags:
admin jquery oldforms disable
Score:
0 (after 0 ratings)

This snippet shows how to disable fields in a edit page in the oldforms admin using jquery.

The idea is to add the javascript to the edit page using the js attribute of the model's Admin class. In this case jQuery and a custom javascript file are added.

The javascript sets the disabled attribute of the name field to true as soon as the document is ready.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Model:

class Placeholder(models.Model):
    name=models.CharField()
    description = models.CharField(blank=True, maxlength=200)
 
    class Admin:
        list_display = ('name', 'description')
        js = (
              '/appmedia/js/jquery.js',
              '/appmedia/js/placeholder.js',
              )

placeholder.js:

$(document).ready(function(){
   $('#id_name').attr("disabled", true);
});

More like this

Comments

miguelvel18 (on October 8, 2009):
<p>when the field is disabled,web form shows a error message of validation because the field is required. The field have a default value, but shows a error message validation, help me</p>

#

Please login first before commenting.