本文介绍了从加载的信息中计算出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要能够为每个用户都有的投注显示动态回报,但由于某些原因,他们都无法运作。我以前曾经问过这个问题,但没有运气。 我希望在这个额外的细节将足以帮助最终得到答案。 我使用 odds-1,stake-1 等硬编码了其中一个脚本,并且这个工作,但其他人没有。 如果有人可以帮助,这将不胜感激。 JavaScript var count_div = 0; $(div)。each(function(){ count_div ++; console.log(Counter:+ count_div); }); $(变量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 $ b 函数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%'>参与者:< / td>< td> 。 stripslashes($ result ['participant'])。 < / TD>< / TR> 中; $ return。=< tr>< td>市场:< / td>< td> 。 stripslashes($ result ['market'])。 < / TD>< / TR> 中; $ return。=< tr>< td>时间:< / td>< td> 。日期(H:i,strtotime($ result ['bet_till_time']))。 < / TD>< / TR> 中; $ return。=< tr>< td>赔率:< / td>< td> 。 $ result ['赔率']。 < input type ='hidden'value ='。 $赔率。 'id ='__ odds-。 $ idCount。 '/>< / td>< / tr>; $ return。=< tr>< td> Stake:< / td>< td>< div class ='input-group'>< span class ='input-group- addon'id ='basic-addon1'>& pound;< / span>< input style ='width:100%'type ='text'name ='stake'id ='stake-。 $ idCount。 ''aria-describeby ='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:& pound;< / span>< input style ='width:100%'type ='text'id ='showdynamicreturn- 。 $ idCount。 ''aria-describeby ='basic-addon2'placeholder ='0.00'只读/>< / div>< / center>< / td>< / tr>; if($ result ['ew_available'] ==true){ $ return。=< tr>< td>< center>< label>方式下注& nbsp;& nbsp;< 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 ='下注'/>< / center>< / td>< / tr>; } $ return。=<! - 马信息的隐藏字段 - >; $ 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 ='赔率'值='。 $ result ['赔率']。 '/>; $ 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 \并从投注单中移除一个项目 - >; $ return。=< tr>< td colspan ='100%'>< center>< input type ='submit'name ='delete_betslip_item'value ='删除此赌注onclick = \return confirm('您确定要删除吗?')\/>< / center>< / td>< / tr>; $ return。=< / table>; $ return。=< / form>; $ return。=< / div>< br>; } $ return。=< / div>; 返回$ return; } 生成的HTML < div style ='border:1pt solid black;宽度:99%; border-radius:25pt;'> < h2>塞浦路斯v威尔士 - 比赛投注< / h2> < form action ='/'method ='post'> < table class ='table table-condensed'> < tr> < td style ='width:50%;'> Sport:< / td> < td>足球< / td> < / tr> < tr> < td style ='width:50%'>参与者:< / td> < td>塞浦路斯< / td> < / tr> < tr> < td>市场:< / td> < td>欧元2016< / td> < / tr> < tr> < td>时间:< / td> < td> 19:45< / td> < / tr> < tr> < td>赔率:< / 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'>& pound;< / span>< input style ='width'100%'type ='text'name ='stake'id ='stake-3'aria-describeby ='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:& pound;< / span>< input style ='width:100%'type ='text'id ='showdynamicreturn-4'aria-describeby ='basic-addon2'placeholder ='0.00'readonly />< / div><<< ; /中心],[ < / td> < / tr> < tr> < td colspan ='10'> < center>< input type ='submit'name ='submitTo__OpenBets'value ='Place Bet'/>< / center> < / td> < / tr> < 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 =''/><! - 将区域设置为提交删除并从投注单中移除项目。 - >< TR> < td colspan ='100%'> < center>< input type ='submit'name ='delete_betslip_item'value ='删除这个赌注'onclick =return confirm('您确定要删除这个吗?')/> < /中心],[ < / td> < / tr> < / table> < / form> < / div> 解决方案好吧,我明白了。 因此,将类分配给以下标记 < input type ='hidden'value ='3.8'id ='__ odds-3'/> < input style ='width:100%'type ='text'name ='stake'id ='stake-3'aria-describeby ='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 =oddstype ='hidden'value ='3.8 'id ='__ odds-3'/> < input class =stakestyle ='width:100%'type ='text'name ='stake'id ='stake-3'aria-describedby ='basic-addon1'placeholder ='Stake '/> < input class =showdynamicreturnstyle ='width:100%'type ='text'id ='showdynamicreturn-4'aria-describeby ='basic-addon2'placeholder ='0.00'readonly /> 之后,我们可以使用最近的使用jquery并添加一个类特定的功能。它会完成剩下的工作。 所以javascript代码看起来像这样 var newVal =(()){code> $(document).ready(function(){ $ parseFloat($(this).val(),10)* parseFloat($(this).closest(。table)。find(。odds).val(),10))+ parseFloat($ ).val(),10)|| 0; $ b $(this).closest(。table)。find(。showdynamicreturn).val(parseFloat(newVal).toFixed 2)); }); }); 无论您拥有多少个表格,此代码都适用于所有表格,它会给出结果只在期望的估计收益文本框上 工作小提琴在这里 http ://jsfiddle.net/6fgw91zx/ 只需查看它并让我知道它是否有用 I need to be able to show a "dynamic return" for each bet that the user has in place, but for some reason none of them work. I have previously asked this question before, but with no luck.I am hoping that the extra detail in this will be sufficient to help with getting an answer to this at last.I have hard coded one of the scripts in so to use odds-1, stake-1etc, and this worked but the others have not.If anybody can help with this it would be greatly appreciated.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)); }); }PHPfunction 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;}Generated 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> 解决方案 Ok I got it. Instead of adding functions with id specific in the DOM we can use class specific.So assign classes to your following tags<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 />Add Classes dynamically when you are generating this HTML tags by your javascript. just assign it. So the tags should be look like the below<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 />After that we can use Closest using jquery and add a class specific function. It'll do the rest.So the javascript code will be look like this$(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)); });});This code will work for all the tables no matter how many you have and it'll give the result only on the desired estimated return textboxThe working fiddle is here http://jsfiddle.net/6fgw91zx/Just check it and let me know if it is helpful 这篇关于从加载的信息中计算出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-03 23:13