2011-02-26 94 views

回答

-1

使用removeAttr函数执行此操作。

对于readonly将属性readonly="readonly"添加到文本并将其从脚本中删除。

<script> 
function func1(button) 
{ 
    $(button).val('Save'); 
    $('input[type=text]').removeAttr('disabled') 
} 
</script> 
<input type="text" disabled="disabled"></input> 
<input type="button" onclick="func1(this)" value="enable"/> 

演示here

+1

问题问只读,不能禁用。 – Eli 2011-02-26 06:33:09

+0

@Eli这么大以至于它必须被低估? – 2011-02-26 06:34:52

+0

如果用户尝试使用Ajax发布此表单,则不会发布该字段的表单值。需要只读。 – Eli 2011-02-26 06:36:49

1

这里的上的jsfiddle http://jsfiddle.net/KTYWT/演示

HTML

<input id="textbox" type="text" readonly="readonly" /> 
<input type="button" id="textbutton" value="Edit" /> 

jQuery的

$('#textbutton').click(function(e) { 
    var text = $('#textbox'); 

    if (text.is('[readonly]')) { 
     text.removeAttr('readonly'); 
     $(this).val('Save'); 
    } else { 
     text.attr('readonly', 'readonly'); 
     $(this).val('Edit'); 
    } 
}); 
+0

感谢此守则正在工作 – akila 2011-03-03 07:53:56

-1

为例e。使用jQuery的位置:http://jsfiddle.net/jomanlk/azGPf/

<input id='thebox' type='text' readonly="readonly" value='locked'> 
<button id='thebutton'>The Button</button> 

$('#thebutton').click(function(){ 
    $('#thebox').removeAttr('readonly'); 
    $(this).html('save'); 
}) 

重命名按钮,使文本编辑。

编辑答案编辑以显示“只读”作为礼指出

+1

只有只读的问题,未禁用。 – Eli 2011-02-26 06:32:41

+0

我认为这个问题主要集中在改变什么属性的文本属性上。 +1取消downvote – 2011-02-26 06:37:36

+0

同意和回报:) – JohnP 2011-02-26 06:38:36