[英]How can I make this button in my website redirect to another page on the website?
I have been trying to make this button redirect to another page on the website based on some of the research I've done here and in other places, but no matter what I try, it won't work. 我一直试图根据我在这里和其他地方所做的一些研究,将这个按钮重定向到网站上的另一个页面,但无论我尝试什么,它都行不通。 As a preface, this website is django-based, but I also have bootstrap, javascript, and JQuery in here.
作为序言,这个网站是基于django的,但我也有bootstrap,javascript和JQuery。
<button type="submit" class="btn btn-link" onclick="window.location.href='create-student'">Sign up</button>
So, the Sign up button is on the base page and create-student is the link to the page I want the button to go to. 因此,注册按钮位于基页上,create-student是指向我希望按钮转到的页面的链接。 That's what I have so far, among other things I've tried.
这就是我到目前为止所做的事情,以及我尝试过的其他事情。 It simply refreshes the page and adds a ?
它只是刷新页面并添加一个? to the end of the url.
到网址的末尾。
As you are using bootstrap, you can style a link to look like a button: 在使用引导程序时,您可以将链接设置为看起来像按钮:
<a href="create-student" class="btn">Sign up</a>
This also saves problem of no javascript 这也节省了没有javascript的问题
You should make it as a link, and style it to look like a button. 你应该将它作为一个链接,并将其设置为看起来像一个按钮。
With boostrap, this is accomplished by adding class="btn"
to a link. 使用boostrap,可以通过将
class="btn"
添加到链接来实现。
After that it is much simpler, no javascript tricks involved and you are using links when those should be used. 之后它更简单,没有涉及javascript技巧,你在使用链接时使用链接。 (Navigating from page to another).
(从页面导航到另一个)。
<a href="create-student" class="btn btn-default">Sign up</a>
If you are curious how to style it as you wan't read more from the docs: 如果你很好奇如何设计它,因为你不会从文档中阅读更多内容:
Boostrap docs about buttons. 关于按钮的Boostrap文档。
Basically, you can use any of the btn-default
, btn-primary
, btn-success
and similar classes for the links also. 基本上,您也可以使用任何
btn-default
, btn-primary
, btn-success
和类似的类作为链接。
Cheers. 干杯。
To expand on ChrisC's (and now Maunos - damn I'm too slow at typing) answer... 为了扩展ChrisC(现在是Maunos - 该死的我打字太慢了)回答......
Can you not just make your an instead without needing to use Javascript? 你不能只需要做一个而不需要使用Javascript吗?
<a class="btn btn-default" href="/create-student">Sign Up</a>
The advantage of using an <a>
with a href
is that it is symantically correct (it's a link tag, and you are creating a link), users without Javascript can use it (yes I know this is a very small issue these days but still), and it is better for people who want to right click and go Open link in new tab'. 使用带有
href
的<a>
的优点是它在语义上是正确的(它是一个链接标记,你正在创建一个链接),没有Javascript的用户可以使用它(是的我知道这是一个非常小的问题,但是对于想要右键单击并在新标签中打开链接的人来说,这样更好。 Try doing that with a javascript based button, you can't. 尝试使用基于javascript的按钮,你不能。
I see it mentioned that you might want this to be a submit. 我看到它提到你可能希望这是一个提交。 From the sounds of it I don't see that, but if it does need to be a submit it should be within a form, and the
action
will do the post to that particular page, eg 从它的声音我看不到,但如果它确实需要是一个提交它应该在一个表单内,并且该
action
将发布到该特定页面,例如
<form action="create-student">
<!-- Your form goes here -->
<input type="submit" value="Sign Up" class="btn btn-default" />
</form>
Add a ID to your button 为您的按钮添加ID
<button id="btnSubmit" type="submit" class="btn btn-link" >Sign up</button>
$("#btnSubmit").click(function()
{
window.location.href = "http://stackoverflow.com"; //Your URi here
}):
MORE NOTE: I hope using a button type Submit to redirect to another page is odd , if you want to just redirect the user on button click, use button type instead of Submit type like below 更多注意:我希望使用按钮类型提交重定向到另一个页面是奇怪的 ,如果你只想重新定向用户点击按钮,使用按钮类型而不是像下面提交类型
<input id="btnSubmit" type="button" class="btn btn-link" value="Sign Up">
(OR) (要么)
Use a tag also like <a href="create-student" class="btn">Sign up</a>
使用
<a href="create-student" class="btn">Sign up</a>
等标记
Hope this helps... 希望这可以帮助...
Try... 尝试...
onclick="window.open('create-student')"
instead. 代替。
The default behavior of the button element is to submit. button元素的默认行为是提交。 Try cancelling the default behavior adding
return false
. 尝试取消添加
return false
的默认行为。
HTML HTML
<button type="submit" class="btn btn-link" onclick="redirect">Sign up</button>
JS JS
function redirect() {
window.location.href='create-student';
return false; // cancel default behavior
}
Considering it is a Django based site, and extending the anwer of @ChrisC, the correct way of doing it, would be: 考虑到它是一个基于Django的网站,并扩展了@ChrisC的修正,正确的做法是:
Give a name to the url that you want to access 为要访问的URL指定名称
url(r'^signup/$', your_view.SignUp.as_view(), name="signup"),
Use the name of the url on the href
tag in your template 使用模板中
href
标记上的url名称
<a class="btn" href="{% url 'signup' %}">Sign up</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.