[英]how to redirect to another page when using javascript
I have a form In their it has a cancel button.我有一个表格,里面有一个取消按钮。 when that button is clicked a confirm box will appear and ask whether user needs to leave the page then i need to redirect the page .I'm using codeigniter and I did this but it is redirects to same page.I don't know why?当该按钮被点击时,将出现一个确认框并询问用户是否需要离开页面然后我需要重定向页面。我正在使用 codeigniter 并且我这样做了,但它被重定向到同一页面。我不知道为什么? can anyone help me?谁能帮我?
View(busineeRateView.php)查看(busineeRateView.php)
<?php $Vehicleid=$details['id']; ?>
<input type="submit" name="cancelreview" class="btn btn-primary btn-lg" value="CANCEL" onClick="return cancelConfirm();">
<script>
function cancelConfirm() {
job = confirm("Are you sure you want to cancel and leave this page?");
if (job == true) {
window.location.href = "http://localhost/ci/adpreview_ctrl/getad_preview/".$Vehicleid;
return true;
}
}
</script>
controller控制器
public function loadReviewPage($vehicleid){
$data=array();
$data['details']['id']=$vehicleid;
$this->load->view('pages/templates/header');
$this->load->view('pages/businessRateView',$data);
$this->load->view('pages/templates/footer');
} }
Since you return true
from the event handler, the submit button submits the form.由于您从事件处理程序return true
,因此提交按钮会提交表单。
Since that comes after the location
assignment, it overwrites it.由于这是在location
分配之后,它会覆盖它。
Submitting a form will reload the current page (assuming form doesn't have an action
attribute and the submitted data doesn't cause the server to return a different page).提交表单将重新加载当前页面(假设表单没有action
属性并且提交的数据不会导致服务器返回不同的页面)。
Don't use a submit button.不要使用提交按钮。 Your JavaScript is doing little more than simulating a link, so use a real link.您的 JavaScript 只是模拟链接,因此请使用真实链接。
<a class="btn btn-primary btn-lg"
href="http://localhost/ci/adpreview_ctrl/getad_preview/$Vehicleid"
onclick="return cancelConfirm();">
Cancel
</a>
and和
function cancelConfirm(){
return confirm("Are you sure you want to cancel and leave this page?");
}
NB: You seem to have left some PHP in your example.注意:您的示例中似乎留下了一些 PHP。 Make sure you express your URL in valid HTML/JS.确保您以有效的 HTML/JS 表达您的 URL。
this line is confusing:这一行令人困惑:
window.location.href = "http://localhost/ci/adpreview_ctrl/getad_preview/".$Vehicleid;
Seems you mix javascript and php.似乎您混合使用 javascript 和 php。 Concatenating strings in JS is done with +, not . JS 中的字符串连接是用 + 完成的,而不是 . which is php syntax.这是 php 语法。 and php variables like $Vehicleid are not accessible directly in JS.和 $Vehicleid 之类的 php 变量不能直接在 JS 中访问。 Try to use a php block for that.尝试为此使用 php 块。 like:喜欢:
window.location.href = "http://localhost/ci/adpreview_ctrl/getad_preview/" + "<?= $Vehicleid; ?>";
I think maybe you use the wrong operation.我想也许你使用了错误的操作。 You should use '+' instead of '.'您应该使用“+”而不是“.” in javascript string operation.在javascript字符串操作中。 '.' '.' is used in php:)在 php 中使用:)
window.location.href = "http://localhost/ci/adpreview_ctrl/getad_preview/" + $Vehicleid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.