2014-12-23 78 views
-1

世界上最簡單的PHP表單不提交。我只想要一個用戶點擊圖像時提交的表單。目前絕對沒有任何反應。使用Javascript:爲什麼這個簡單的AJAX PHP表單不提交?

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> 
<script> 
function changeImage(id) { 
document.images[id].src = "x"; 
var datastring = 'orderID=13&partID=' + id; 
submit; 

$.ajax 
(
    { 

    type: "POST", 
    url: "allocatepartssubmit.php", 
    data: dataString , 
    cache: false, 
    success: function(){ 
     alert('success'); 
    }, 
    error: function(){ 
     alert('failure'); 
    } 
    }); 

} 

</script> 

相關的HTML行:

<img src="x" onclick="changeImage('x')" id="x"> 

我已經取代了各個部分用 'X' 爲便於觀看。 js圖像開關工作正常,所以我知道正在調用changeimage()函數。但表單不是發佈。它沒有提示成功或失敗,暗示.ajax的內容沒有被看到。

Wat do?

+6

'submit;'不會調用任何...順便說一句,你在說FORM嗎??? –

+0

如果你想提交表單使用這個'document.getElementById(「myForm」)。submit();' –

+0

道歉的傢伙,但我已經找到了問題!這是我有datastring和dataString。而已!是的,'提交'這一行來自以前的一段代碼,現在它是多餘的。對不起浪費你的時間! – mcplums

回答

1

試試吧,甜美&短代碼給你!

<img src="exapmle.png" class="change_image" id="x"> 

的jQuery -

$(document).ready(function() { 

    $('.change_image').live('click',function(){ 

     var id = $(this).attr('id'); 
     var dataStr = 'orderID=13&partID=' + id; 

     $.ajax({ 
      type: "POST", 
      url: "allocatepartssubmit.php", 
      data: dataString , 
      cache: false, 
      success: function(response){ 
       alert('success'); 
      }, 
      error: function(err){ 
       alert('failure'); 
      } 
     }); 
    }); 
}); 
0

試試這個它會工作:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> 
<script> 
function changeImage(id) { 
var datastring = 'orderID=13&partID=' + id; 

$.ajax 
(
    { 

    type: "POST", 
    url: "allocatepartssubmit.php", 
    data: dataString , 
    cache: false, 
    success: function(){ 
     alert('success'); 
    }, 
    error: function(){ 
     alert('failure'); 
    } 
    }); 

} 

</script> 

從您的代碼刪除submit;document.images[id].src = "x";,因爲這些未使用。