[英]Chrome javascript/jquery redirects to index
EDIT: I searched around meanshile and read that chrome doesn't allow local files, could that be the reason? 编辑:我在Meanhile周围搜索,并读取chrome不允许本地文件,这可能是原因吗?
I have created my first website including mostly php and jQuery and some JavaScript, everything works fine on Firefox, my problem is on chrome whenever I submit a form in a specific tab it runs everything but then redirects to index instead of printing the result. 我创建了我的第一个网站,主要包含php和jQuery以及一些JavaScript,在Firefox上一切正常,当我在特定选项卡中提交表单时,我的问题出在chrome上。
Here are my functions 这是我的职能
function cshoptab(tab) { // for register.php
var tab = tab;
switch (tab) {
case 'weapons':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/weapons.php');
break;
case 'helms':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/helms.php');
break;
case 'suits':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/suits.php');
break;
case 'gloves':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/gloves.php');
break;
case 'boots':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/boots.php');
break;
case 'style':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/style.php');
break;
case 'costumes':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/costumes.php');
break;
case 'pets':
$("#cshopwrap").html('Loading').show();
$("#cshopwrap").load('php/cshop/pets.php');
break;
}
}
function cshopsenditem(idxvalue, opt, duration) {
$("#content").html('Loading purchase').show();
var idxvalue = idxvalue.split('x');
var idx = idxvalue[0];
var price = idxvalue[1];
var url = "php/cshop.php";
$.post(url, {idx: idx, price: price, duration: duration, opt: opt}, function(data) {
$("#content").html(data).show();
});
}
PHP: PHP:
<?php
include 'functions.php';
session_start();
if($_SESSION['username']){
$username=$_SESSION['username'];
if($_POST){
$price=$_POST['price'];
$idx=$_POST['idx'];
$opt=$_POST['opt'];
$duration=$_POST['duration'];
$cashstring = cshop($username, $opt, $duration, $idx, $price);
echo $cashstring;
}
?>
FORM: 形成:
<form name=form method='post' onsubmit='cshopsenditem(document.getElementById("idx").value,
null,
document.getElementById("duration").value)'>
<table>
<tr>
<td rowspan=9><img src=cimg/style.png></td>
<td>Item: </td>
<td>
<select name=idx id='idx' onchange='updateprice(document.getElementById("idx").value)'>
<option value=901x100>Change Kit - Hair Style (Novice)</option>
<option value=904x100>Change Kit - Hair Color (Normal)</option>
<option value=906x100>Change Kit - Face (Novice)</option>
</select>
</td>
</tr>
<tr>
<td>Restriction: </td>
<td>Account Binded</td>
</tr>
<tr>
<td>Duration: </td>
<td>Permanent</td>
</tr>
<tr>
<td>Price: </td>
<td><div id='price'>100</div></td>
</tr>
<tr>
<td colspan=2 class='cshopbuybutton'>
<input name=duration id='duration' type=hidden value=31>
<input name=buybutton type=submit class='button' value='BUY NOW'>
</td>
</td>
</table>
</form>
Maybe because you don't want to refresh your page... If I'm wrong, just ignore it. 可能是因为您不想刷新页面...如果我错了,请忽略它。
$.post(url, {idx: idx, price: price, duration: duration, opt: opt}, function(data) {
$("#content").html(data).show();
});
return false; //Missed this line
When you're submitting a form from $.post
or $.ajax
you don't need to specify onSubmit()
method in form
, try giving an id
for your form and execute code on $('#myform').click(function(){});
从$.post
或$.ajax
提交表单时,不需要在form
指定onSubmit()
方法,请尝试为form
提供一个id
,然后在$('#myform').click(function(){});
上执行代码$('#myform').click(function(){});
对于较晚的答案,我感到抱歉,其原因是因为Google chrome由于某些原因不允许onsubmit事件,因此我不得不将其更改为onclick。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.