0

希望有人能阐明下面的代码,它总是提醒下面的错误。该代码旨在验证一个单选按钮是否被选中,否则返回 false 到当前页面,否则继续执行表单操作。提前谢谢你!!

  function onDisplayItemsForm(){
  var re = false;           // used to determine when a button is checked
  var radIdSelected = frmDisplayItems; 

// traverse the radio buttons
// if one is checked sets re to true, and stops the iteration
    for(var i=0; i<radIdSelected.length; i++) 
    {
      if(radIdSelected[i].checked == true) 
      {
       re = true;
   break;

       }

   if (!radIdSelected[i].checked) 
{
      alert("Please select product");
      return false;
    }
     return true;

  }
 };

表格如下:

  <form name="frmDisplayItems" action="showItem.php" onsubmit="return onDisplayItemsForm();" >


      <table width="50%" border="1">

  <th>Country of Origin</th>
  <th>Select</th>
  </tr>


         <td><input name=\"radId\" type=\"radio\" value=\"$id\" /></td>



  </tbody>
  </table>

  <p><input name="btnSubmit" type="submit" value="Select"/>  </p>
  </form>

<

4

1 回答 1

0

这是re您要在循环完成后检查的值:

if (!re)
{
    alert("Please select product");
    return false;
}

return true;
于 2013-02-17T02:23:07.183 回答