我有一些应该是单选按钮的图像,用户应该选择并继续使用页面,但是不起作用,它总是返回“请标记产品”。
PHP代码
$shoppingTable = array(
array("radioValue" => 001, "url_image" => "/images/a.png", "Price" => 19.99, "Coins" => 25, "Package" => 1),
array("radioValue" => 002, "url_image" => "/images/b.png", "Price" => 34.99, "Coins" => 40, "Package" => 2),
array("radioValue" => 003, "url_image" => "/images/c.png", "Price" => 49.99, "Coins" => 80, "Package" => 3),
array("radioValue" => 004, "url_image" => "/images/d.png", "Price" => 89.99, "Coins" => 125, "Package" => 4),
array("radioValue" => 005, "url_image" => "/images/e.png", "Price" => 124.99, "Coins" => 150, "Package" => 5),
);
echo '<form action="http://'.$_SERVER['HTTP_HOST'].'/?view=donatecoins&action=check" method="post" onsubmit="return validaDados(this);"><input type="hidden" name="method" value="1"/>'; '
foreach($shoppingTable as $i => $shop) {
echo '<div id="'.$shop['Package'].'" class="product coin">
<span class="title">
<span class="amount">'.$shop['Coins'].'</span>
<br>
coins
</span>
<div class="product_coin_image" style="background-image: url('.$shop['url_image'].');"></div>
<div class="price">R$ '.$shop['Price'].'</div>
<input type="radio" name="ProdValor_1" id="ProdValor_1" value="'.$shop['radioValue'].'"/>
</div>';
}
echo '</form>';
JavaScript函数
function validaDados(frm)
{
var f;
for (i=frm.ProdValor_1.length-1; i > -1; i--) {
if (frm.ProdValor_1[i].checked) {
f = true;
break;
}
}
if(!f) {
alert("Please mark a product.");
return false;
}
}
答案 0 :(得分:-2)
您可以尝试使用php的isset函数。检查单选按钮是否已选中。如果选中单选按钮,则isset函数返回true。
<?php
f=false;
if (isset($ProdValor_1))
{
f=true;
echo "A product is checked";
}
if(!f) {
echo "Please mark a product.";
}
?>