最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove readonly attribute when button is clicked - Stack Overflow

programmeradmin5浏览0评论

How can I remove the readonly attribute when the Edit button is clicked? My codes is working if clicked but after one second the input form is back to readyonly.

<input class="form-control" name="fullname" value="<php echo $fullname; ?>" readonly/>
<button class="btn btn-primary" id="btnEdit" > edit </button>

<script>
    $(document).ready(function(){
        $('#btnEdit').click(function(){
            $("input[name='fullname']").attr("readonly", false);   
        });
    });
</script>

How can I remove the readonly attribute when the Edit button is clicked? My codes is working if clicked but after one second the input form is back to readyonly.

<input class="form-control" name="fullname" value="<php echo $fullname; ?>" readonly/>
<button class="btn btn-primary" id="btnEdit" > edit </button>

<script>
    $(document).ready(function(){
        $('#btnEdit').click(function(){
            $("input[name='fullname']").attr("readonly", false);   
        });
    });
</script>
Share Improve this question edited May 28, 2016 at 9:31 Manfred Radlwimmer 13.4k13 gold badges55 silver badges64 bronze badges asked May 28, 2016 at 9:06 expert123expert123 376 silver badges11 bronze badges 5
  • button lead to a form submit? – gu mingfeng Commented May 28, 2016 at 9:11
  • I would think that you have some other code which make the field read only. You better to find what code is responsible for making the fields read only. Maybe the form gets updated with ajax request? – Mikhail Chibel Commented May 28, 2016 at 9:14
  • api.jquery./removeAttr - readonly is a boolean attribute meaning its presence is all that is needed to work. – Niet the Dark Absol Commented May 28, 2016 at 9:15
  • removeAttr is not the right method. Use .prop instead. Example $(element).prop('readonly', false). – php-dev Commented May 28, 2016 at 9:24
  • Possible duplicate of how we add or remove readonly attribute from textbox on clicking radion button in cakephp using jquery? – Rajesh Commented May 28, 2016 at 9:29
Add a ment  | 

2 Answers 2

Reset to default 4

You should use jQuery removeAttr

$(document).ready(function(){
    $('#btnEdit').click(function(){
        $("input[name='fullname']").removeAttr( "readonly" ); 
    });
});

try this you have to use removeAttr reference link

<script>
$(document).ready(function()
{
 $('#btnEdit').click(function()
 {
   $("input[name='fullname']").removeAttr("readonly");  
 });

 });

 </script>
发布评论

评论列表(0)

  1. 暂无评论