我正在尝试根据使用jQuery更改的类的名称来操作数量。我的金额来自这里:
<script>
var inboundAmount = <?php echo json_encode($inbound); ?>;
</script>
我们只说数量是10。
我输出了一些像这样的php和脚本:
echo "<a href=\"#inbound\" class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound', $vendor)\">$<script>document.write(inboundAmount);</script></a>";
当用户点击它时,该类会发生变化。示例类如下:
half first3547
pending first3547
paid first3547
我一直试图做的是,如果类包含单词'half',那么它会做一些数学运算:
document.write(inboundAmount/2);
产生数字5.我试过这个和一些变化没有运气:
if ($("#mycontent").find("a.half").length > 0) {document.write(inboundAmount/2);}
'mycontent'是href存在的表的id。
我也尝试过以下方法:
if ( $( this ).hasClass( 'half' ) ) { {document.write(inboundAmount/2);}
仍无济于事。
一个完整的例子是这样的:
<td class='grav-results-td'>
<?php if (!empty($check_inbound)):echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound',
$vendor)\">$<script>if ( $( this ).hasClass( 'half' ) )
{document.write(inboundAmount/2);}</script></a>"; endif; ?></td>
而且我也在我的ajax脚本的成功区域尝试了操作。
回答以下一些问题:
$ inbound_status第一个$ id是一个PHP变量。
document.write(inboundAmount / 2)的输出;正常工作并显示document.write(inboundAmount)的一半;
this
标签内的<script>
范围不会指向您期望的元素。而不是检查javascript你可以检查php
本身的条件。否则,您可以参考以下代码在javascript上执行此操作。您可以使用php变量来检查它是否包含字符串half
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">
$<script>if ('$inbound_status' == 'half' )
{document.write(inboundAmount/2);}
</script>
</a>";
endif;
echo "</td>";
如果inboundAmount
是一个php变量,你可以轻松地完成它而不使用javascript
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">$";
if ($inbound_status == 'half'):
echo $inboundAmount/2;
endif;
echo "</a>";
endif;
echo "</td>"