开发者

Put a field as read-only with javascript or jquery

开发者 https://www.devze.com 2023-04-03 06:53 出处:网络
I have tried to set a item to read only, as you see in the code i have tried several way and can\'t get that to work. Can anyone help me with this?

I have tried to set a item to read only, as you see in the code i have tried several way and can't get that to work. Can anyone help me with this?

<html>
<script>

 //test.Attribute开发者_如何学Cs.Add("readonly","readonly")

 //document.getElementById('testtt').setAttribute('readonly', 'readOnly'); 

 // document.getElementByID('test').value=readOnly;

 //document.getElementByID('test').readOnly = true;

 // $("#test").attr("readonly", "readonly");

 //$("#test").removeAttr("readonly");

 //$('#test').attr('readonly', 'readonly');

 $("#test").attr("readonly")

</script>
<body>  
     <input id="test" type="text" value="text" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</body>
</html>


$('#test').attr('readonly', true);

working example : http://jsfiddle.net/FBUDt/


this should work for you:

 $("#test").attr("readonly", "readonly");

remember that the DOM needs to be loaded before you try to find the element

you could use jquerys DOM-ready

$(function() {
    $("#test").attr("readonly", "readonly");
});

what version of jquery are you using?


You should try wrapping your code into $('document').ready();

According to the documentation,

The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

So it would be:

$('document').ready(function(){
    $("#test").attr("readonly", "readonly");
});


You can't refer to an element that is created later in the document. Move the script down below the form, or place it in a document.ready block. Also, you should use $.prop() in jQuery 1.6+

$(function(){
  $('#test').prop('readonly', true);
});


I would go with

document.getElementById('test').setAttribute('readonly', 'readOnly');

But that is not the issue. The position of your script is where it goes wrong. You are trying to change element which does not exist.

<!DOCTYPE html>
<html>
   <head>
     <meta charset=utf-8 />
     <title>testcase</title>
     <link rel="stylesheet" href="stylesheet.css" type="text/css" />
  </head>
  <body>
    <div class="some container">
      <input id="test" type="text" value="text" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
     </div>
     <script type="text/javascript" charset="utf-8">
       document.getElementById('test').setAttribute('readonly', 'readOnly');
     </script>
  </body>
</html>

If you place your scripts right befor the closing </body> tag , then it is executes as soon as DOM has been built.

And please. Stop using JS libraries to do the most basic things.


you are missing

$(document).ready(function() {
  $('#test').attr('readonly', 'readonly');
}); 

sample

http://fiddle.jshell.net/Pe4J5/

0

精彩评论

暂无评论...
验证码 换一张
取 消