PHP Javascript Variable problem
I have a while loop in my PHP page where I look the following...
print $divLeft.strip_tags($row->twitterUser)."?size=normal\"/><br \/>".$row->twitterUser.$divRight."<a href='javascript:void(0);' id='vote_$row->id' class='getPoint'>Get " .$row->coff. "<input type='hidden' value='$row->coff' id='credoff'/> credit(s)</a><br /></div>$clearDiv</div></div>";
I have a value set in my hidden field which I then call in my javascript...
{
var theid = $(this).attr("id");
var onlyID = theid.split("_");
var onlyID = onlyID[1];
credoff = $("#credoff").val();
$.ajax({
url: 'do.php',
type: 'POST',
data: "userID=" + onlyID,
success: function(data) {
if(data != "success1" && data != "success5") {
$("#" + theid).text(data);
}else{
$("#thediv_" + onlyID).fadeOut("slow");
$('#creditsBalance').fadeOut("slow");
newbalance = parseInt($('#creditsBalance').text());
if(data != "success5") {
开发者_如何学Python alert('Credits offered = '+credoff);
The only thing is in my javascript its grabbing the highest 'credoff' variable value on the page, not the one clicked on, does this make sense?
First off, $('#credoff') is an id... and IDs are unique to a page. Secondly, if there were more than one (and built properly through a CSS selector), the way you're calling $('#credoff') would only give you the value of the first one, since you're not associating it with the event target.
Since it looks structurally like the input is a child of the (which is odd, too, btw), you'll need to use a selector like this to get credOff:
$('.getPoint').click(function(){
// properly associated with the event target.
var credOff = $(this).find('input').val();
// etc.
}
Element IDs must be unique throughout the page; if they are not, then attempting to select by ID is undefined. Your best bet is to use something like this:
<input type="hidden" ... class="credoff" />
And then in the javascript something like this:
credoff = $(this).children('input.credoff:hidden').val()
精彩评论