admin管理员组文章数量:1323200
I'm trying to figure this one out but my mind has just gone blank.
- I have a button element on my webpage with an id: e.g
<button id="someId"></button>
- In the document.ready function, I have an on click event which occurs when the user clicks the button. e.g.
$('#someId').on('click', function() {
- In this event there is an if statement which determines a value. Depending on the value, I want to execute a href in an anchor but I can't figure out the syntax.
The reason I am trying to execute the anchor in the javascript is because i'm passing a variable into the href. It could be true or false.
Here is what I have\what I'm trying to do. Any help would be great.
if (date1 <= date2) {
//I want to execute this anchor
<a href="@{Application.openthispage(true)}"></a>
}else{
//otherwise execute this anchor
<a href="@{Application.openthispage(false)}"></a>
}
I'm trying to figure this one out but my mind has just gone blank.
- I have a button element on my webpage with an id: e.g
<button id="someId"></button>
- In the document.ready function, I have an on click event which occurs when the user clicks the button. e.g.
$('#someId').on('click', function() {
- In this event there is an if statement which determines a value. Depending on the value, I want to execute a href in an anchor but I can't figure out the syntax.
The reason I am trying to execute the anchor in the javascript is because i'm passing a variable into the href. It could be true or false.
Here is what I have\what I'm trying to do. Any help would be great.
if (date1 <= date2) {
//I want to execute this anchor
<a href="@{Application.openthispage(true)}"></a>
}else{
//otherwise execute this anchor
<a href="@{Application.openthispage(false)}"></a>
}
Share
Improve this question
asked Feb 1, 2013 at 19:42
EddieEddie
6214 gold badges13 silver badges23 bronze badges
2 Answers
Reset to default 6You're looking at the problem wrong. An anchor is a static HTML element that, when clicked, changes the current window location (i.e. URL). The JS equivalent looks something like this: window.location.href = 'http://www.google.';
. So by setting window.location.href
you will use JS to navigate to another URL.
The window.location method posted by Eli is the correct way to visit a link using JavaScript. However, if you can use a link instead of a button, you could just set the href of the link in the onclick. Here is a sample jsFiddle that visits a different url based on whether one input is greater than the other: http://jsfiddle/jw3Pd/
So when the user clicks the link, set the href to whatever link you want the user to visit.
$("#someId").on("click", function () {
if (date1 <= date2) {
//I want to execute this anchor
$("#someId").attr("href", "@{Application.openthispage(true)}");
} else{
//otherwise execute this anchor
$("#someId").attr("href", "@{Application.openthispage(false)}");
}
});
本文标签: jqueryHow to execute an anchorhref in my javascriptStack Overflow
版权声明:本文标题:jquery - How to execute an anchorhref in my javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742095831a2420542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论