admin管理员组文章数量:1426115
I have the following:
HTML
<div class="tab-pane" id="message">
<textarea rows="4" cols="50" id="send_message" placeholder="Enter text ..."> </textarea>
<a href="#message" class="btn btn-large btn-info" data-toggle="tab">OK</a>
<a href="#message" class="btn btn-large btn-info" data-toggle="tab">Cancel</a>
JavaScript
$('#message').click(function(){
if($("a", this).is(":contains(OK)")) {
console.log("im in OK!!");
} else if($("a", this).is(":contains(Cancel)")) {
console.log("im in cancel!!");
}
});
This works fine when I hit the OK button and executes as expected, however when I hit cancel the code in OK is executed only. The cancel code never executes! What am I doing wrong?
I have the following:
HTML
<div class="tab-pane" id="message">
<textarea rows="4" cols="50" id="send_message" placeholder="Enter text ..."> </textarea>
<a href="#message" class="btn btn-large btn-info" data-toggle="tab">OK</a>
<a href="#message" class="btn btn-large btn-info" data-toggle="tab">Cancel</a>
JavaScript
$('#message').click(function(){
if($("a", this).is(":contains(OK)")) {
console.log("im in OK!!");
} else if($("a", this).is(":contains(Cancel)")) {
console.log("im in cancel!!");
}
});
This works fine when I hit the OK button and executes as expected, however when I hit cancel the code in OK is executed only. The cancel code never executes! What am I doing wrong?
Share Improve this question edited Aug 12, 2013 at 18:19 rink.attendant.6 46.5k64 gold badges110 silver badges157 bronze badges asked Aug 12, 2013 at 18:13 user1592380user1592380 36.6k105 gold badges314 silver badges553 bronze badges 2-
Why should it execute if
a:contains('OK')
still exists in a#message
? – u_mulder Commented Aug 12, 2013 at 18:17 - 2 It never enter the else if since $("a", this) contains both OK and Cancel – zs2020 Commented Aug 12, 2013 at 18:18
3 Answers
Reset to default 9this
is the element the event is bound to. It's always #message
, therefore $("a", this)
will always be the both buttons. .is
will scan both buttons to see if any of them contain "OK"
. The first one does, so it always goes to the 1st block.
You should be binding the events to the buttons themselves, not the entire div.
Detect clicks on a
from #message
, instead. this
will bee the clicked <a>
.
$('#message').on("click", "a", function(){
if($(this).is(":contains(OK)")) {
console.log("im in OK!!");
}
else if($(this).is(":contains(Cancel)")) {
console.log("im in cancel!!");
}
});
JSFIDDLE
Fist, I noticed you have a missing closing div (</div>
)
Just curious, why not do the following:
$("#OK").click (function() {
console.log("im in OK!!");
});
$("#Cancel").click (function() {
console.log("im in Cancel!!");
});
<div class="tab-pane" id="message">
<textarea rows="4" cols="50" id="send_message" placeholder="Enter text ..."> </textarea>
<a href="#message" id="OK" class="btn btn-large btn-info" data-toggle="tab">OK</a>
<a href="#message" id="Cancel" class="btn btn-large btn-info" data-toggle="tab">Cancel</a>
</div>
Cheers.
本文标签: jqueryJavascript else if statement not workingStack Overflow
版权声明:本文标题:jquery - Javascript else if statement not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745408791a2657371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论