删除只读属性

时间:2016-03-07 15:54:23

标签: jquery

如果文本字段包含其他值可编辑,我需要一个只读文本字段。

这就是我做的事情

if ($("#prq_purcatalogkey").val() != null) {

    $('#prqitms_purcatalogd').prop('readonly', true);
}
else {
    $('#prqitms_purcatalogd').removeProp("readonly");

} 

但是readonly无法正常工作,该字段始终只读它是否包含值! 。

我试过了

$('#prqitms_purcatalogd').prop('readonly', true);

$('#prqitms_purcatalogd').removeAttr('readonly');

但没有效果。

6 个答案:

答案 0 :(得分:6)

您可以尝试

$('#prqitms_purcatalogd').prop('readonly', false);

而不是.removeAttr()

正如评论中所建议的那样,您将希望在更改元素时触发此操作,因此请将其附加到.change()事件处理程序。

$("#prq_purcatalogkey").change( function(){
if ( $(this).val() == '' ){
    $('#prqitms_purcatalogd').prop('readonly', false);
} else {
    $('#prqitms_purcatalogd').prop( "readonly", true );
} 

答案 1 :(得分:4)

如果文本框包含使用以下代码的值,则可以使文本框只读。这是基于您的评论。

if ($("#prqitms_purcatalogd").val() != '') {

    $('#prqitms_purcatalogd').attr('readonly', true);
}
else {
    $('#prqitms_purcatalogd').attr("readonly", false);

} 

我有JSFiddle代码here

答案 2 :(得分:1)

要删除readonly属性,请执行以下操作:

$('#prqitms_purcatalogd').prop("readonly", false); // Element(s) are now enabled.

希望这有帮助

答案 3 :(得分:1)

尝试以下方法......

var fieldValue = $("#prq_purcatalogkey").val();
if (fieldValue != null && fieldValue != "") {
 $('#prqitms_purcatalogd').attr('readonly', 'true');
}else{
 $('#prqitms_purcatalogd').removeAttr('readonly');
}

或者还有一种方式。

 var fieldValue = $("#prq_purcatalogkey").val();
    if (fieldValue != null && fieldValue != "") {
     $('#prqitms_purcatalogd').prop("disabled", false);
    }else{
     $('#prqitms_purcatalogd').prop("disabled", true);
    }

答案 4 :(得分:1)

因为许多人都写了Function GetDestination(Target As Range, Optional default_value As Variant) AS Variant 'Lists the Hyperlink Address or Subaddress for a Given Cell 'If cell does not contain a hyperlink, return default_value If (Target.Cells(1, 1).Hyperlinks.Count <> 1) Then GetDestination = default_value Else With Target.Cells(1, 1).Hyperlinks(1) If Len(.Address)>0 Then GetDestination = .Address Else GetDestination = .SubAddress End If End With End If End Function ,所以我想提一句这行不通。 以只读方式显示时,它指定输入字段为只读,即= true,= false,=任何内容。

使用JQuery的正确方法是$('selector').attr("readonly", false);

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly

答案 5 :(得分:0)

我尝试了上述所有解决方案,但似乎对我没有任何帮助,但我设法找到了一种解决方法,它是使用纯JavaScript。

尽管这个问题已经存在了将近3年,但我希望这个答案能触及任何可能遇到此问题的人。

 document.getElementById("prqitms_purcatalogd").readOnly = false;