jquery按钮只在一篇文章中启用
jquery button enabling just in one post
我有一系列的帖子。每个帖子都有一个textarea
(输入文本)和一个取消和提交按钮。最初,提交按钮被禁用,直到写入一些文本。
然而,当我第一次在一个textarea
中键入一些文本时,所有提交button
的帖子都会被启用,而不仅仅是帖子中键入的那个。
我的JavaScript代码:
$('.small-textarea').click(function() {
$('.btn-info').attr('disabled','disabled');
$(this).addClass("set-large");
$('.set-large').keyup(function() {
var $this = $('.set-large');
if($(this).val() == '') {
$('.btn-info').attr('disabled','disabled');
}
else {
$('.btn-info').removeAttr('disabled');
}
});
});
HTML:
<div id="footer-condensed-replies" class="footer-condensed">
<!--<input class="input-xlarge" type="text" placeholder="Reply to @Gerardo">-->
<textarea class="small-textarea" type="text" placeholder="Reply to @Gerardo"></textarea>
<div id="" class="footer">
<div id="" class="footer-submit-button">
<button type="button" id="cancel" class="btn" data-value="Cancel">Cancel</button> <span id="footer-btn-margin"></span>
<button id="hunch" class="btn btn-info" type="button" data-value="Submit">Submit</button>
</div>
</div>
</div>
在看不到标记的情况下,这是一种方法:
假设你的标记是这样的:
<input type="text" class="small-textarea" />
<input type="button" class="btn-info" value="Submit" />
<br/>
<input type="text" class="small-textarea" />
<input type="button" class="btn-info" value="Submit" />
<br/>
<input type="text" class="small-textarea" />
<input type="button" class="btn-info" value="Submit" />
您可以这样做:由于按钮在文本框旁边,您将使用.next()
来选择并仅针对该按钮,而不是针对所有按钮。
$('.small-textarea').click(function () {
$(this).addClass("set-large").next('.btn-info').prop('disabled', true);
});
$(document).on('keyup', '.set-large', function () { // Use another container that holds these elements in instead of document
$(this).next('.btn-info').prop('disabled', $(this).val() == '');
});
使用
prop
而不是attr
不要在未解除绑定的情况下在另一个事件中注册click事件,它最终会为元素创建重复的keyup处理程序。您可以使用事件委派,而不是使用
.on()
1.7+或.live()
旧版。使用相对目标,而不是使用可以选择所有目标的类名进行选择。
更新
有了你的标记,你可以这样做:
$('.small-textarea').click(function () {
$(this).addClass("set-large").next().find('.btn-info').prop('disabled', $(this).val() == '');
});
$(document).on('keyup', '.set-large', function () {
var $this = $(this);
$(this).next().find('.btn-info').prop('disabled', $(this).val() == '');
});
演示
使用prop()而不是attr()。。。。和$(this).parent().find('.btn-info')
以获得提交按钮
试试这个
$('.small-textarea').click(function() {
$('.btn-info').prop('disabled',true);
$(this).addClass("set-large");
});
$('.set-large').keyup(function() {
if($(this).val() == '') {
$(this).parent().find('.btn-info').prop('disabled',true);
}
else {
$(this).parent().find('.btn-info').prop('disabled',false);
}
});
相关文章:
- 使用ajax在多个页面上发布一篇文章
- 计算一篇文章中的节数(不止一篇文章)
- 计算一篇文章中有多少节
- 如何提供指向'下一篇文章'
- 翻译一篇文章'上.单击'PHP中的事件,来自'window.open'到基本URL链接
- 我怎么能只用Javascript(AJAX)编写其中一篇文章呢
- 单击我的joomla网站上的模块标题以指向一篇文章
- 定位页面上的第一篇文章和最后一篇文章
- jquery按钮只在一篇文章中启用
- 如何点击按钮显示下一篇文章
- 将变量数据从.js的一篇文章中抓取到php中
- 每回一篇文章,页面都会向后滚动
- 如何获取我的博客上最后一篇文章的日期,并将其显示在另一个网站上?
- 在领英上发表一篇文章
- 使用Tumblr API获取下一篇或上一篇文章
- 在展开下一篇文章时折叠文章(Readmore.js - Extension)
- 如何滚动一个大的表面?(例如一篇文章)
- Angular Typeahead:点击建议并立即发表一篇文章
- 如何水平滚动到页面中的下一篇文章(或#等)
- 如何在网页上为网页上的每一篇文章单独添加facebook分享按钮