开发者

Ajax to read from form

I'm writing a store system for my game, it worked quite well until I found out that it only takes the amount of first entered Item.

function pbuy(buyitem) {
 var amountc = "amount-"+buyitem,
 var amount = $("#"+amountc+"").val();
   $.ajax({
   type: "POST",  
   url: "store2.php",  
   data: "buyitem="+ buyitem+"&amount="+amount,  
   success: function(resp){  
   document.getElementById('ajaxDiv').innerHTML =  resp;

   },  
   error: function(e){  
     alert('Error: ' + e);  
   }  
 });  
}  

I'm trying to give it it the Id of the form like so:

function p开发者_Go百科buy(buyitem) { var amountc = "amount-"+buyitem, var amount = $("#"+amountc+"").val();

But nothing happens.

The code the creation of the forms is:

<tr> 
    <td class='items' width='80%' align='center' valign='top'>
        <?PHP echo $itemstore->itemname;?> 
    </td> 
    <td width="20%">  
        Price:<?PHP echo $itemstore->newprice;?>          
        <form  method="post"> 
            <input type='text' id='amount-<?PHP echo $row;?>)' />   
            <input name="itembid" type="button" onclick="pbuy(<?PHP echo $row;?>)"  value="Buy" />
        </form>
    </td> 
</tr>

If I hardcode the amount in the ajax function it all runs fine like it should.


You're getting a javascript error before the AJAX-request is being sent since you are defining your amountc and amount variables separated by a comma. Either do

 var amountc = "amount-"+buyitem;
 var amount = $("#"+amountc+"").val();

semicolon separated, or keep the comma and skip the second var

 var amountc = "amount-"+buyitem,
 amount = $("#"+amountc+"").val();


Did you checked the response of the AJAX request?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜