如何使用javascript在所有输入字段上添加readonly属性?

时间:2021-08-07 07:28:05

I want to add readonly attribute on all input fields using javascript. I have done for single input field using this $('#someid').prop('readonly', true); but I want to add to all input field in Edit Mode.

我想使用javascript在所有输入字段上添加readonly属性。我使用这个$('#someid')为单输入字段做了.prop('readonly',true);但我想在编辑模式下添加到所有输入字段。

Input Fields

<div class="form-group">
    <label for="Barcode">Barcode: </label>
    <input name="Barcode" id="Barcode" class="form-control" value="<?=$data['Barcode']?>" placeholder="Barcode">
</div>
<div class="form-group">
    <label for="ApplicantName">ApplicantName: </label>
    <input name="ApplicantName" id="ApplicantName" class="form-control" value="<?=$data['ApplicantName']?>" placeholder="ApplicantName">
</div>
<div class="form-group">
    <label for="SubBarcode">Reference No: </label>
    <input name="SubBarcode" id="SubBarcode" class="form-control" value="<?=$data['SubBarcode']?>" placeholder="Reference No">
</div>
<div class="form-group">
    <label for="ClientName">Client Name: </label>
    <input name="ClientName" id="ClientName" class="form-control" value="<?=$data['ClientName']?>" placeholder="Client Name">
</div>
<div class="form-group">
    <label for="ClientRefNo">Client Reference No: </label>
    <input name="ClientRefNo" id="ClientRefNo" class="form-control" value="<?=$data['ClientRefNo']?>" placeholder="Client Reference No">
</div>
<div class="form-group">
    <label for="DateOfBirth">Date Of Birth: </label>
    <input name="DateOfBirth" id="DateOfBirth" class="datetimepicker-month form-control" value="<?=$data['DateOfBirth']?>" placeholder="Date Of Birth">
</div>
<div class="form-group">
    <label for="PassportNo">Passport No: </label>
    <input name="PassportNo" id="PassportNo" class="datetimepicker-month form-control" value="<?=$data['PassportNo']?>" placeholder="Passport No">
</div>
<div class="form-group">
    <label for="AKAName">AKAName: </label>
    <input name="AKAName" id="AKAName" class="datetimepicker-month form-control" value="<?=$data['AKAName']?>" placeholder="AKAName">
</div>
<div class="form-group">
    <label for="Gender">Gender: </label>
    <input name="Gender" id="Gender" class="datetimepicker-month form-control" value="<?=$data['Gender']?>" placeholder="Gender">
</div>
<div class="form-group">
    <label for="Nationality">Nationality: </label>
    <input name="Nationality" id="Nationality" class="datetimepicker-month form-control" value="<?=$data['Nationality']?>" placeholder="Nationality">
</div>
<div class="form-group">
    <label for="ArabicName">Arabic Name: </label>
    <input name="ArabicName" id="ArabicName" class="datetimepicker-month form-control" value="<?=$data['ArabicName']?>" placeholder="Arabic Name">
</div>

Is there any short way that i can dynamically add read-only attribute to all input fields.

有没有简短的方法可以动态地将只读属性添加到所有输入字段。

4 个解决方案

#1


5  

You can use element selector with attribute value selector to select all the textboxes and apply the readonly property to all of the selected elements.

您可以使用带有属性值选择器的元素选择器来选择所有文本框,并将readonly属性应用于所有选定元素。

$('input[type="text"]').prop('readonly', true);

To select all the textboxes in the form

选择表单中的所有文本框

$('yourFormSelector input[type="text"]').prop('readonly', true);

As an alternative to attribute value selector, you can also use :text selector to select all textboxes.

作为属性值选择器的替代方法,您还可以使用:文本选择器来选择所有文本框。

$(':text').prop('readonly', true);

#2


3  

You can use class for that

你可以使用类

$('.form-control').prop('readonly', true);

#3


1  

you can specify a common class (recommended) or just globally target all the input elements in the form (not recommended)

你可以指定一个公共类(推荐)或只是全局定位表单中的所有输入元素(不推荐)

$('input[type="text"]').prop('readonly', true);

#4


1  

Try this:

尝试这个:

$('.classname').attr('readonly', 'readonly');

OR you can use this:

或者您可以使用此:

$('.classname').prop('readonly', true);

$('。classname')。prop('readonly',true);

#1


5  

You can use element selector with attribute value selector to select all the textboxes and apply the readonly property to all of the selected elements.

您可以使用带有属性值选择器的元素选择器来选择所有文本框,并将readonly属性应用于所有选定元素。

$('input[type="text"]').prop('readonly', true);

To select all the textboxes in the form

选择表单中的所有文本框

$('yourFormSelector input[type="text"]').prop('readonly', true);

As an alternative to attribute value selector, you can also use :text selector to select all textboxes.

作为属性值选择器的替代方法,您还可以使用:文本选择器来选择所有文本框。

$(':text').prop('readonly', true);

#2


3  

You can use class for that

你可以使用类

$('.form-control').prop('readonly', true);

#3


1  

you can specify a common class (recommended) or just globally target all the input elements in the form (not recommended)

你可以指定一个公共类(推荐)或只是全局定位表单中的所有输入元素(不推荐)

$('input[type="text"]').prop('readonly', true);

#4


1  

Try this:

尝试这个:

$('.classname').attr('readonly', 'readonly');

OR you can use this:

或者您可以使用此:

$('.classname').prop('readonly', true);

$('。classname')。prop('readonly',true);