点击按钮后,jQuery Prop 不会禁用
jquery prop doesn't disabled after clicked button
我有一个关于jquery prop的问题。我从 codepen.io 创建了这个演示
在此演示中,您可以看到有两个带有 id 的编辑按钮。当我单击第一个编辑按钮时,它工作正常。但是编辑按钮也可以第二次单击,我想禁用当前单击的按钮。我在这里缺少什么,任何人都可以在这方面帮助我?
.js
$(document).ready(function() {
$("body").on("click", ".editBtn", function() {
var ID = $(this).attr("id");
$('#ser' + ID).prop('disabled', 'true');
var currentMessage = $("#messageB" + ID + " .postInfo").html();
var editMarkUp = '<textarea rows="5" cols="80" id="txtmessage_' + ID + '">' + currentMessage + '</textarea><button name="ok">Save</button><button name="cancel">Cancel</button>';
$("#messageB" + ID + " .postInfo").html(editMarkUp);
});
});
.html
<div class="container">
<div class="postAr" id="messageB1">
<div class="postInfo">
fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsa
</div>
<button class="editBtn" name="edit" id="ser1">Edit</button>
</div>
</div>
<div class="container">
<div class="postAr" id="messageB2">
<div class="postInfo">
fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsass
</div>
<button class="editBtn" name="edit" id="ser2">Edit</button>
</div>
</div>
您不是针对button
.使用$(this).prop()
$(this).prop('disabled', 'true');
或
执行所需操作的 JavaScript 方法是公开 event
对象并设置 event.target
的 disabled
属性:
$(document).ready(function() {
$("body").on("click", ".editBtn", function(event) {
event.target.disabled = true;
...
});
});
更新的代码笔
当您使用此选择器时$('.editBtn' + ID).prop('disabled', 'true')
您没有任何带有editBtn1 or editBtn2 class
的按钮,所以我只使用了id selector
,
检查下面的代码片段
$(document).ready(function() {
$("body").on("click", ".editBtn", function() {
var ID = $(this).attr("id");
$('#' + ID).prop('disabled', 'true');
var currentMessage = $("#messageB" + ID + " .postInfo").html();
var editMarkUp = '<textarea rows="5" cols="80" id="txtmessage_' + ID + '">' + currentMessage + '</textarea><button name="ok">Save</button><button name="cancel">Cancel</button>';
$("#messageB" + ID + " .postInfo").html(editMarkUp);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="postAr" id="messageB1">
<div class="postInfo">
fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsa
</div>
<button class="editBtn" name="edit" id="1">Edit</button>
</div>
</div>
<div class="container">
<div class="postAr" id="messageB2">
<div class="postInfo">
fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsass
</div>
<button class="editBtn" name="edit" id="2">Edit</button>
</div>
</div>
相关文章:
- JQuery prop('required', false, (!$(this).is(':checked')) 不起作
- 使用 jQuery 使用 .prop 获取无线电值
- jquery .prop('defaultSelected') 似乎在 IE9 中出现故障
- 点击按钮后,jQuery Prop 不会禁用
- jQuery 'prop' 在 FireFox 中无法在详细信息的 'open' 属性上工作
- 按钮输入上的 CSS 取消了 jQuery 中禁用的 prop 属性
- 如何使用jQuery的prop函数获取文本框的值
- Jquery 'prop' 使 chrome 在大量复选框上无响应
- JQuery 复选框不会更改 .prop(“checked”, true) 处的视觉状态
- Jquery - .prop 不会动态选中我的输入复选框
- jQuery:禁用 prop IF 选中
- 使用jQuery.prop&对象文字-两种不同的结果
- 从PHP中提取的目录列表中的jQuery prop父复选框
- Jquery.attr和.prop删除Chrome中的属性
- 使用jQuery.attr或.prop设置属性值无效
- 使用jQuery's.prop方法来设置函数的属性
- 使用.prop('selectedIndex',#)的JQuery集选择不起作用
- jQuery.prop在没有错误的情况下停止执行
- jQuery.prop(“disabled”,false)没有启用输入
- 使用hide、prop、attr或css隐藏JQuery元素之间的区别