当前位置: 代码迷 >> JavaScript >> javascript验证空白输入框 不起作用!解决办法
  详细解决方案

javascript验证空白输入框 不起作用!解决办法

热度:549   发布时间:2012-05-05 17:21:10.0
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'>
  </tr>
  </table>
  <p></p>
  <p style='width:90%;color:#6B6B6B; margin:auto; font-family: Verdana, Arial, Helvetica, sans-serif;'><b>Payment Methods (Please select one of them)</b></p>
  <table border='0' style='width:90%;color:#6B6B6B; margin:auto'>
    <tr>
      <td colspan=2 style='font-size:10.5pt'> You have<a style='color:#d34836'> $4.00 </a>credits left.<a style='color:#d34836' href='recharge.php'>Recharge Now!</a></td>
    </tr>
    <tr>
      <td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='credit' disabled='disabled'>
        Pay by Whim Win Credit - <a style='color:#d34836' href='recharge.php'>Recharge</a> will receive <a style='color:red'>5% discount off.</a></td>
    </tr>
    <tr>
      <td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='cr_and_card'>
        $4.00 Whim Win Cr+$20.4 by Credit / Debit Card / Paypal <a href='#' onclick='javascript:window.open('https://www.paypal.com/au/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');'><img  src='https://www.paypalobjects.com/en_AU/i/bnr/horizontal_solution_PP.gif' border='0' alt='Solution Graphics'></a></td>
    </tr>
    <tr>
      <td style='font-size:11.5pt'><input type='radio' name='payment_methods' value='card'>
        Credit / Debit Card / Paypal <a href='#' onclick='javascript:window.open('https://www.paypal.com/au/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');'><img  src='https://www.paypalobjects.com/en_AU/i/bnr/horizontal_solution_PP.gif' border='0' alt='Solution Graphics'></a></td>
    </tr>
  </table>
  <p></p>
  <p style='width:90%;color:#6B6B6B; margin:auto; text-align:center'>
    <input id='btn_continue' style='' name='continue_order' type='submit' value='Continue Shopping'>
    <input id='btn_confirm' style='' name='go_next' type='submit' value='Confirm'>
    <input name='hidden_credit' type='hidden' value='4.00'>
  </p>
  <p></p>
</form>
</div>
<div class="step_num"><b style="color:#d34836">FOLLOWING STEPS</b></div>
</br>
<div class="step_num"><b style="color:#d34836">STEP 4</b>: Pay, and remember your invoice ID, we are waiting for you at your pick up time!</div>
</br>
<div id="footer">
  <p> Offcial Email:<a href="mailto:whimwin@gmail.com">whimwin@gmail.com</a> <br />
    Please feel free to email us for any feedback or suggestions, Thank you!</a> <br />
  <h4>copyright &copy Whimwin Pty Ltd.</h4>
  <br />
</div>

<script type="text/javascript">
function isEmpty() {
    var obj = document.getElementsByTagName('form')[0].getElementsByTagName('input');
    
    var FLAG = {
        pm: false,  //name = "payment_methods"
        otherRadio: false
    }
    
    for (var i = 0; i < obj.length; i ++) {
        if (obj[i].type == 'radio') {
            if (obj[i].name == 'payment_methods') {
                if (obj[i].checked == true) FLAG.pm = true;
            }
            else {
                if (obj[i].checked == true) FLAG.otherRadio = true;
            }
        }
    }
    
    if (!FLAG.pm || !FLAG.otherRadio) {
        alert('Please choose your pick up time and payment method.');
        return false;
    }
    else return true;
}
</script> 
</body>
</html> 
  相关解决方案