Jquery does not direct to another page

Asked

Viewed 235 times

1

I have a website that has some buttons on it. When you click on the buttons, they are directed to the respective pages, but there is a button from which it does not direct, it just stays on the same page. The code I’m using for him is:

    $('#btnSeguroViagem').on("click",function()
          {
              var novaURL = "seguro-viagem.php";
              $(window.document.location).attr('href',novaURL);
          });
<button type="submit" name="Submit" id="btnSeguroViagem" value="Solicitar"  class="btn btn-danger btn-small"><i class="fa fa-plus-square"></i> Solicitar Seguro Viagem</button>

Can anyone tell me why this guy is not going? The page exists, I removed all the content and put a sentence, but nothing. By clicking the button, gives the post, but remains on the same page and does not direct to the safe-travel page.php. The strange thing is that I already gave an Alert() inside the call and arrives correctly.

  • 2

    Good evening, I reversed the question to the original, Jose does not put the answer in the question, to create an answer click on the answer button.

  • Right William... I’ll create the answer.

  • I can’t find the answer button.

  • 1

    Good afternoon Jose, closed questions have no answer button, as your question was a typo, so it is not necessary an answer.

2 answers

2

In this case jQuery is not necessary. The solution is simple javascript. Here are two options:

Option 1:

window.location.href = "http://answall.com";
// ou uma variante com o mesmo efeito
window.location.assign("http://answall.com");

Option 2:

window.location.replace("http://answall.com");

The difference between these two methods is that the first makes it possible to click back on the browser history and go to the previous page. The second replaced the current page and when clicking to go back in history, the homepage is not accessible (it has been replaced in the story).


If you want the user to open a new window (without losing the source window), you can use:

window.open("http://answall.com");

This window.open() method also allows you to pass parameters/options such as size, content, etc.


Source: How to redirect the user to another Javascript/jQuery page?

  • Right Leonardo, but the code I have, uses Jquery, because I’m using the tabs in Jquery.

  • Which is strange, that even putting other pages, it does not direct.

  • 1

    If your links are using # may be the problem, no?

  • Excuse me colleagues. The error is in the type of the button. I am using Submit instead of the button. When using Submit, it gives a post on the page. I traded Submit for Button and it worked. Mind tires. Thank you all.

0

Although you have already found your answer, I believe there are several simpler ways to get the expected result.

To

How are you already making use of the Bootstrap, follows an information available on the page of the framework:

Use the button classes on an < a > , < button >, or < input > element.

This means you can create a tag a as the same look as a button. Example:

<a class="btn btn-danger btn-small" href="seguro-viagem.php">
    <i class="fa fa-plus-square"></i>Solicitar Seguro Viagem
</a>

Using the tag a, you still have the benefit of being able to use the attribute target.

FORM

Tagged form you can call the desired URL using the attribute action. Example:

<form action="seguro-viagem.php">
    <button type="submit" value="Solicitar" class="btn btn-danger btn-small">
        <i class="fa fa-plus-square"></i> Solicitar Seguro Viagem
    </button>
</form>

Javascript

Through the javascript you can access a window object called location. This object has a property called href, value the current window address. By changing this property, you redirect the client to a new address. Example:

location.href = 'seguro-viagem.php';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.