我正在尝试提取特定类的每个项目的每个ID,将其传递给php脚本进行处理,然后将结果返回到项目本身。似乎在大多数情况下都有效,但我会为每个项目获得相同的随机数。
这是jQuery
function loadRenew()
{
$('.renewItem').each(function(){
var currentItem = parseInt($(this).attr('id'));
//$('#loading').css('visibility','visible'); //show the rotating gif animation
$.ajax({ //create an ajax request
type: "POST",
url: "renew.php",
data: 'barcode='+currentItem, //with the page number as a parameter
dataType: "html", //expect html to be returned
success: function(msg){
if(parseInt(msg)!=0) //if no errors
{
$("#"+currentItem).html(msg); //load the returned html
//$('#loading').css('visibility','hidden'); //and hide the rotating gif
}
}
});
});
}
和PHP
<?php
if(!$_POST['barcode']) die("0");
$barcode = (int)$_POST['barcode'];
echo 'barcode returned is: ' . $barcode;
?>
我没有得到当前的条形码(currentItem),但是每个div都有相同的随机数。
处理结果示例:
<div class="panel">
<div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> Politics and cinema / Andrew Sarris. <span style="font-weight: bold;">PN1995.9.P6 S2</span> Stacks</span></div>
<div class="card_info">
<b>Checked Out: </b> <span style="font-family: Courier;"> 02/05/2014</span> \
<p><b>Date Due: </b> <span style="font-family: Courier;"> 05/16/2014</span></p>
<div class="renewItem" id="30291002497476">barcode returned is: 2147483647</div>
</div>
</div>
<div class="panel">
<div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> The statics of particles and rigid bodies. <span style="font-weight: bold;">QA821 .H47</span> Stacks</span></div>
<div class="card_info">
<b>Checked Out: </b> <span style="font-family: Courier;"> 02/06/2014</span>
<p><b>Date Due: </b> <span style="font-family: Courier;"> 05/17/2014</span></p>
<div class="renewItem" id="30291003147765">barcode returned is: 2147483647</div>
</div>
</div>
<div class="panel">
<div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> Tablets <span style="font-weight: bold;"></span> Reserve</span></div>
<div class="card_info">
<b>Checked Out: </b> <span style="font-family: Courier;"> 02/18/2014</span>
<p><b>Date Due: </b> <span style="font-family: Courier;"> 02/21/2014</span></p>
<p><b>Fine: <span style="font-family: Courier; color:red;"> $5.00</span></b></p>
<div class="renewItem" id="30291009878660">barcode returned is: 2147483647</div>
</div>
</div>
<div class="panel">
<div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> Dog day afternoon [videorecording] / Warner Bros Pictures ; an Artists Entertainment Complex product <span style="font-weight: bold;">PN1997 .D63 2006</span> Media DVD</span> </div>
<div class="card_info">
<b>Checked Out: </b> <span style="font-family: Courier;"> 02/05/2014</span>
<p><b>Date Due: </b> <span style="font-family: Courier;"> 02/12/2014</span></p>
<div class="renewItem" id="30291010328143">barcode returned is: 2147483647</div>
</div>
</div>
答案 0 :(得分:1)
这不是随机数。数字2,147,483,647也是计算中32位有符号整数的最大值。因此,它是声明为&#34; int&#34;的变量的最大值。
尝试使用字符串。不要将它解析为int值,这应该可以解决问题。