日期:2014-05-16  浏览次数:20397 次

javascript验证空白输入框 不起作用!
我有两个页面需要验证 一个是点菜输入数量(type=text),另一个是付款方式(type=radio)
代码:
点菜页面:
PHP code

echo "<form name='shopping_cart_form' method='post' action='include/shopping_cart_form.php' onsubmit='return dosubmit()'>";
echo "<div id='item_details' style=''>
      <input class='txt_qty' type='text' name='textbox_qty' id='textbox_qty' />
    <input class='add_button' type='submit' name='btn_add' id='btn_add' value='Add to Cart' />
      </div>";  

echo "</form>";


JScript code

//无论我把这段js放在什么位置,都不起作用,为什么啊?
<script language="javascript" type="text/javascript">
function dosubmit()
{
    if(document.shopping_cart_form.textbox_qty.value=="" || document.shopping_cart_form.textbox_qty.value==0)
    {
        if(document.shopping_cart_form.btn_add.value=="Add to Cart")
        {
            document.shopping_cart_form.textbox_qty.style.borderColor="red";

            alert("please fill all fields!");
            return false;
        }
    }
}

</script>




结账页面:
PHP code

echo "<form name='submit_form' method='post' action='order_confirmation.php' onsumbit='return isEmpty();'>";
//....
while(..){
     echo "<td style='$time_style'><input type='radio' name='$dest_id_time' value='$pickup_time_id'$disable>$pickup_time_formatted</td>";
}

echo "<tr><td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='credit' >Pay by Whim Win Credit </td></tr>";
echo "<tr><td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='card' >Credit / Debit Card / Paypal </td></tr>";
echo "</form>";




JScript code

<script language="javascript">
//由于里面有个radio的名字是动态的,所以根浴网友提示,直接遍历所有radio,来判断没checked的选项,但是为什么不起作用啊!!!!????愁死我了
<!--
function isEmpty()
{
    var input=document.getElementsByTagName("input");
    for(i=0;i<input.length;i++)
    {
        if(input[i].type=="radio")
        {
            if(input[i].checked == false)
             alert('Please choose your pick up time and payment method.');
        }
    }
}
//-->
</script>



------解决方案--------------------
奇怪了,我测试过可以的,你检查一下下面的html代码跟你的有什么不同。
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form name='submit_form' method='post' action='order_confirmation.php' onsubmit='return isEmpty();'>
  <tr>
    <td style='border-bottom:3px #4F8A2B solid; color:#6B6B6B'><input type='radio' name='2' value='1' >
      11:30</td>
    <input type='hidden' name='final_invoice_id' value='8731'>
    <input type='hidden' name='order_time' value='2012-05-03 00:00:45'>
    <input type='hidden' name='pickup_date' value='03/05/2012'>
    <input type='hidden' name='display_total_price' value='24.40'>