jQuery removeAttr,prop,attr不适用于必需'财产
美好的一天,我正在与遇到困难和解决与HTML形式组件的“必需”属性有关的所有问题的每个人接触。就我而言,每当我尝试使用removeAttr,attr或prop方法修改或设置它时,“必需”属性似乎不会更新。
注意:“占位持有人”属性的拆卸tr起作用,但是对于“必需”而言,它却没有。
$(document).ready(function() {
$("select#decision").on('change', function() {
var decValue = $("select#decision").val();
if (decValue == 'For STR') {
$("textarea#addtl_reason").prop('required', true);
$("textarea#add_notes").removeAttr('placeholder');
$("#sos").slideDown();
$("#reason").slideDown();
$("#narrative").slideDown();
}
else {
$("textarea#addtl_reason").removeAttr('required');
$("#sos").slideUp();
$("#reason").slideUp();
$("#narrative").slideUp();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "narrative" class="form-group col-md-6">
<label class="control-label col-md-5" for="addtl_reason">STR Narrative</label>
<div class="col-md-7">
<textarea cam-variable-name="addtl_reason"
cam-variable-type="String"
id="addtl_reason"
maxlength="4000"
class="form-control glow"
rows="5"
aria-describedby="notesHelpBlock"
placeholder="Write your case narrative here..."
onkeyup="cnt_reason_character()"
autofocus></textarea>
<small id="notesHelpBlock" class="form-text pull-right text-muted"> <span id="words_count" class="d-none form-text text-muted"> <span id="reason_textcount">0</span> / 4000</span></small>
<small id="notesHelpBlock" class="form-text text-muted">Provide your case narrative here.</small>
</div>
</div>
Good day, I am reaching out to everyone who has encountered difficulties and resolved issues related to the 'required' property of html form components. In my case, the 'required' property doesn't seem to update whenever I try to modify or set it using removeAttr, attr or prop methods.
Note: the removeAttr for the 'placeholder' property works, however for 'required' it doesn't.
$(document).ready(function() {
$("select#decision").on('change', function() {
var decValue = $("select#decision").val();
if (decValue == 'For STR') {
$("textarea#addtl_reason").prop('required', true);
$("textarea#add_notes").removeAttr('placeholder');
$("#sos").slideDown();
$("#reason").slideDown();
$("#narrative").slideDown();
}
else {
$("textarea#addtl_reason").removeAttr('required');
$("#sos").slideUp();
$("#reason").slideUp();
$("#narrative").slideUp();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "narrative" class="form-group col-md-6">
<label class="control-label col-md-5" for="addtl_reason">STR Narrative</label>
<div class="col-md-7">
<textarea cam-variable-name="addtl_reason"
cam-variable-type="String"
id="addtl_reason"
maxlength="4000"
class="form-control glow"
rows="5"
aria-describedby="notesHelpBlock"
placeholder="Write your case narrative here..."
onkeyup="cnt_reason_character()"
autofocus></textarea>
<small id="notesHelpBlock" class="form-text pull-right text-muted"> <span id="words_count" class="d-none form-text text-muted"> <span id="reason_textcount">0</span> / 4000</span></small>
<small id="notesHelpBlock" class="form-text text-muted">Provide your case narrative here.</small>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试Vanilla JavaScript:
如果您在jQuery对象上使用Vanilla JavaScript方法,它将无法识别它。因此,您要么使用JavaScript方法引用元素(推荐)或退参考jQuery对象:
零表示您仅针对第一个目标,因此,如果您已经知道这是第二个输入使用索引1。
只是一些建议,您要删除的属性通常不会因为其性质而被删除。
占位符
永远不需要删除,因为当用户专注于表单控件时,它会消失。必需的
不应删除,因为它有助于验证。您为什么需要删除它们?Try vanilla JavaScript:
If you use vanilla JavaScript methods on a jQuery object, it will not recognize it. So you either use JavaScript methods to reference an element (recommended) or de-reference the jQuery object:
The zero indicates that you are targeting the first one only so if you already know that it's the second input use index 1.
Just some advice, the attributes you are removing are not commonly removed because of their nature. A
placeholder
never needs to be removed because it disappears when the user focuses on a form control.required
shouldn't be removed because it facilitates validation. Why would you need to remove them?