我需要能够展现出动态回归"对于用户已经到位的每次下注,但由于某种原因,它们都不起作用。我以前曾问过这个问题,但没有运气。
我希望这里的额外细节足以帮助我最终得到答案。
我已经对其中一个脚本进行了硬编码,以便使用odds-1, stake-1
等,这样做有效但其他人没有。
如果有人可以提供帮助,我们将不胜感激。
的JavaScript
var count_div = 0;
$("div").each(function () {
count_div++;
console.log("Counter: " + count_div);
});
for (var i = 0; i < count_div; ++i) {
$("#stake-" + i).on('keyup', function () {
var newVal = (parseFloat($("#stake-" + i).val(), 10) * parseFloat($("#__odds-" + i).val(), 10)) + parseFloat($("#stake-" + i).val(), 10) || 0;
$("#showdynamicreturn-" + i).val(parseFloat(newVal).toFixed(2));
});
}
PHP
function readbets( $link, $id, $currentpage, $loggedIn = true ) {
$idCount = 0;
$queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
if($loggedIn == true) {
$queryBase2 = sprintf($queryBase, $id);
}
else {
$queryBase2 = sprintf($queryBase, $id);
}
$selectQuery = $link->query($queryBase2);
$return = "<div style='max-height: 680px; overflow: auto;'>";
while($result = mysqli_fetch_array($selectQuery)) {
$idCount++;
if($result['odds'] == "SP") {
$odds = "SP";
}
else {
$_odds = explode("/", $result['odds']);
$odds = $_odds[0] / $_odds[1];
}
$return .= "<div style='border: 1pt solid black; width: 99%;'>";
$return .= "<h2>" . stripslashes($result['title']) . "</h2>";
$return .= "<form action='/' method='post'>";
$return .= "<table class='table table-condensed'>";
$return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
$return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
$return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
$return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
$return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
$return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>£</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
$return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: £</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";
if($result['ew_available'] == "true") {
$return .= "<tr><td><center><label>Each way bet? <input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" . "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
else {
$return .= "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
$return .= "<!-- Hidden fields for the horses information. -->";
$return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
$return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";
$return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
$return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";
$return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
$return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";
$return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
$return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
$return .= "</table>";
$return .= "</form>";
$return .= "</div><br>";
}
$return .= "</div>";
return $return;
}
生成的HTML
<div style='border: 1pt solid black; width: 99%; border-radius: 25pt;'>
<h2>Cyprus v Wales - Match Betting</h2>
<form action='/' method='post'>
<table class='table table-condensed'>
<tr>
<td style='width: 50%;'>Sport: </td>
<td>Football</td>
</tr>
<tr>
<td style='width: 50%'>Participant: </td>
<td>Cyprus</td>
</tr>
<tr>
<td>Market: </td>
<td>Euro 2016</td>
</tr>
<tr>
<td>Time: </td>
<td>19:45</td>
</tr>
<tr>
<td>Odds: </td>
<td>19/5<input type='hidden' value='3.8' id='__odds-3' /></td>
</tr>
<tr>
<td>Stake: </td>
<td>
<div class='input-group'><span class='input-group-addon' id='basic-addon1'>£</span><input style='width: 100%' type='text' name='stake' id='stake-3' aria-describedby='basic-addon1' placeholder='Stake' /></div>
</td>
</tr>
<tr id='dynamic_return'>
<td colspan='10'>
<center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: £</span><input style='width:100%' type='text' id='showdynamicreturn-4' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center>
</td>
</tr>
<tr>
<td colspan='10'>
<center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center>
</td>
</tr>
<!-- Hidden fields for the horses information. -->
<input type='hidden' name='betslip_id' value='13' /><input type='hidden' name='sport' value='' /><input type='hidden' name='eachWay' id='eachWay' value='' /><input type='hidden' name='odds' value='19/5' /><input type='hidden' name='ew_odds' value='' /><input type='hidden' name='sport' value='Football' /><input type='hidden' name='bettilldate' value='' /><input type='hidden' name='bettilltime' value='' /><!-- Area to "submit a delete" and remove an item from the bet slip. --><tr>
<td colspan='100%'>
<center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick="return confirm( 'Are you sure you want to delete this?' )" /></center>
</td>
</tr>
</table>
</form>
</div>
答案 0 :(得分:4)
我认为问题在于你的“for”循环中变量i的使用。您在闭包中使用此变量 - 如果您将alert(i)放入keyup回调中,它将始终为您提供div的总数,无论您更改了哪个值。
要解决这个问题,我建议你以更聪明的方式绑定keyup方法,如下所示:
$("body").delegate('.stake', 'keyup', function () {
var $input = $(this);
var i = parseInt($input.attr('id').replace('stake-', ''));
var newVal = (parseFloat($input.val(), 10) * parseFloat($("#__odds-" + i).val(), 10)) + parseFloat($input.val(), 10) || 0;
$("#showdynamicreturn-" + i).val(parseFloat(newVal).toFixed(2));
});
要使其正常工作,您需要在输入元素中添加“stake”类
答案 1 :(得分:2)
正如你在函数中声明了$ idCount = 0,每次调用函数时都会重新赋值为0.所以为了让它按照我们想要的方式工作,我想在函数外初始化$ idCount值为零的全局变量原型一次以上调用“readbets”函数。它可能是这样的。 我不确定这是否是你想要做的,但我认为这是有帮助的。
$idCount=0;
//not clear enough if its for multiple users uncomment the foreach loop
//foreach ($id as $id){
//each time the idCount incements and the value is stored for next increment.
echo readbets( $link, $id, $currentpage, $loggedIn);
//}
//below is same as your code except that i removed $idCount = 0;
function readbets( $link, $id, $currentpage, $loggedIn = true ) {
//$idCount = 0;
$queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
if($loggedIn == true) {
$queryBase2 = sprintf($queryBase, $id);
}
else {
$queryBase2 = sprintf($queryBase, $id);
}
$selectQuery = $link->query($queryBase2);
$return = "<div style='max-height: 680px; overflow: auto;'>";
while($result = mysqli_fetch_array($selectQuery)) {
$idCount++;
if($result['odds'] == "SP") {
$odds = "SP";
}
else {
$_odds = explode("/", $result['odds']);
$odds = $_odds[0] / $_odds[1];
}
$return .= "<div style='border: 1pt solid black; width: 99%;'>";
$return .= "<h2>" . stripslashes($result['title']) . "</h2>";
$return .= "<form action='/' method='post'>";
$return .= "<table class='table table-condensed'>";
$return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
$return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
$return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
$return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
$return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
$return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>£</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
$return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: £</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";
if($result['ew_available'] == "true") {
$return .= "<tr><td><center><label>Each way bet? <input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" . "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
else {
$return .= "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
$return .= "<!-- Hidden fields for the horses information. -->";
$return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
$return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";
$return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
$return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";
$return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
$return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";
$return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
$return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
$return .= "</table>";
$return .= "</form>";
$return .= "</div><br>";
}
$return .= "</div>";
return $return;
}
或者您可以简单地检查它的设置及其值是否为0,如果不是这样,则将其设置为零。虽然它看起来很漂亮,但每次都会检查是否分配了它。
function readbets( $link, $id, $currentpage, $loggedIn = true ) {
//just added this if else to your code nothing else
$idCount = isset($idCount)?$idCount:$idCount=0;
//edit end
$queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
if($loggedIn == true) {
$queryBase2 = sprintf($queryBase, $id);
}
else {
$queryBase2 = sprintf($queryBase, $id);
}
$selectQuery = $link->query($queryBase2);
$return = "<div style='max-height: 680px; overflow: auto;'>";
while($result = mysqli_fetch_array($selectQuery)) {
$idCount++;
if($result['odds'] == "SP") {
$odds = "SP";
}
else {
$_odds = explode("/", $result['odds']);
$odds = $_odds[0] / $_odds[1];
}
$return .= "<div style='border: 1pt solid black; width: 99%;'>";
$return .= "<h2>" . stripslashes($result['title']) . "</h2>";
$return .= "<form action='/' method='post'>";
$return .= "<table class='table table-condensed'>";
$return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
$return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
$return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
$return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
$return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
$return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>£</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
$return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: £</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";
if($result['ew_available'] == "true") {
$return .= "<tr><td><center><label>Each way bet? <input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" . "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
else {
$return .= "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
}
$return .= "<!-- Hidden fields for the horses information. -->";
$return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
$return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";
$return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
$return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
$return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";
$return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
$return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";
$return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
$return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
$return .= "</table>";
$return .= "</form>";
$return .= "</div><br>";
}
$return .= "</div>";
return $return;
}
答案 2 :(得分:1)
最可能的问题是idcount和i
无法正确匹配 - 可能是因为代码中有其他div。
您可以通过在HTML中包含所有相关内容(idcount)来大规模简化您正在执行的操作并删除出现错误的循环。
更改输出的HTML以在数据元素中包含“id number”idCount
,而不是通过标记ID。 e.g。
// replace the input element with the one below:
<input style='width: 100%' type='text' name='stake' id='stake' data-idnumber='" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' />
然后你可以按如下方式运行你的jQuery选择器(没有任何循环 - 下面的一行替换上面提供的整个代码块)
$("#stake").on('keyup', function () {
var i = parseInt($(this).data("idnumber") , 10);
var orginalVal = parseFloat($(this).val(), 10);
var newVal = orginalVal * parseFloat($("#__odds-" + i).val(), 10)) + orginalVal || 0;
$("#showdynamicreturn-" + i).val(parseFloat(newVal).toFixed(2));
});
注意:
答案 3 :(得分:1)
好的我明白了。我们可以使用特定于类的函数来代替在DOM中添加特定于id的函数。
因此,请将类分配给以下标记
<input type='hidden' value='3.8' id='__odds-3' />
<input style='width: 100%' type='text' name='stake' id='stake-3' aria-describedby='basic-addon1' placeholder='Stake' />
<input style='width:100%' type='text' id='showdynamicreturn-4' aria-describedby='basic-addon2' placeholder='0.00' readonly />
通过javascript生成此HTML标记时动态添加类。只是分配它。所以标签应该如下所示
<input class="odds" type='hidden' value='3.8' id='__odds-3' />
<input class="stake" style='width: 100%' type='text' name='stake' id='stake-3' aria-describedby='basic-addon1' placeholder='Stake' />
<input class="showdynamicreturn" style='width:100%' type='text' id='showdynamicreturn-4' aria-describedby='basic-addon2' placeholder='0.00' readonly />
之后我们可以使用jquery使用Closest
并添加一个特定于类的函数。它会完成剩下的工作。
所以javascript代码看起来像这样
$(document).ready(function() {
$(".stake").on('keyup', function () {
var newVal = (parseFloat($(this).val(), 10) * parseFloat($(this).closest(".table").find(".odds").val(), 10)) + parseFloat($(this).val(), 10) || 0;
$(this).closest(".table").find(".showdynamicreturn" ).val(parseFloat(newVal).toFixed(2));
});
});
此代码适用于所有表格,无论您有多少表格,它只会在所需的估算回报文本框中显示结果
工作小提琴在这里http://jsfiddle.net/6fgw91zx/
检查一下,让我知道它是否有帮助
答案 4 :(得分:0)
检查此工作jsFiddle
改变了一些代码,如下面的
$(document).ready(function() {
$("#stake-3").on('keyup', function () {
var newVal = (parseFloat($("#stake-3").val(), 10) * parseFloat($("#__odds-3").val(), 10)) + parseFloat($("#stake-3").val(), 10) || 0;
$("#showdynamicreturn-4").val(parseFloat(newVal).toFixed(2));
});
});
检查并告诉我它是否有帮助