无法提交包含多个div id的表单...?请帮我

时间:2013-03-30 17:59:56

标签: javascript html forms

无法提交具有多个div id的表单,当我只有一个div id表单正在提交时我使用多个div id表单未提交。我想在按下单选按钮时显示新字段,为了隐藏和可见性,我使用div id(四个div id具有不同的id名称)请帮助

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
    <title>Report Generation</title>
    <link rel="stylesheet" media="screen" href="../css/formstyle.css">
    <script language="javascript" type="text/javascript">
    function setVisible(id, visible) {
        var o = document.getElementById(id);
        if (typeof(o) != 'undefined') o.style.visibility = visible ? 'visible' : 'hidden';
        if (typeof(o) == 'undefined') alert("Element with id '" + id + "' not found.");
    }

    function setDisplay(id, visible) {
        var o = document.getElementById(id);
        if (typeof(o) != 'undefined') o.style.display = visible ? 'block' : 'none';
        if (typeof(o) == 'undefined') alert("Element with id '" + id + "' not found.");
    }
    </script>
</head>

<body   onLoad="setVisible('Div4', false); setVisible('Div1', false);setVisible('Div3', false);setVisible('Div2', false);">
<form class="reg_form" action="pr.php" method="post" name="reg_form">

<ul>
    <li>
        <p>What type of Report you want to Create ?</p><br/>
     </li> 
     <li>
        <input type='radio' name='myradio' value='1' onclick="setVisible('Div1', true); setVisible('Div2', false);setVisible('Div3', false);setVisible('Div4', false);" />Based on Date<br/>
        <input type='radio' name='myradio' value='2' onclick="setVisible('Div2', true); setVisible('Div1', false);setVisible('Div3', false);setVisible('Div4', false);"  />Based on Income<br/>
        <input type='radio' name='myradio' value='3' onclick="setVisible('Div3', true); setVisible('Div1', false);setVisible('Div2', false);setVisible('Div4', false);" />Based on District<br/>
        <input type='radio' name='myradio' value='4' onclick="setVisible('Div4', true); setVisible('Div1', false);setVisible('Div3', false);setVisible('Div2', false);" />Based on Age<br/>
    </li>
    <li>
    <div id='Div1'>

                        <p>From Date:</p> <input type="date" name= "from" required><br/>
                        <p>To Date:</p> <input type="date" name= "to" required><br/>

    </div>
    </li>
    <li>

    <div id='Div2'>
                        <p>Select Slab:</p>
                        <input type="radio" name="slab" value="s1">Slab-1<br/>  
                        <input type="radio" name="slab" value="s2">Slab-2<br/>
            <input type="radio" name="slab" value="s3">Slab-3<br/>
    </div>
    </li>
    <li>
    <div id='Div3'>
                        <p>Enter District:</p><br/>   
                        <input type="text" name= "dist" required>
    </div>
    </li>
    <li>
    <div id='Div4'>
                        <p>Select Age From:</p><br/>     
                        <input type="number" name= "afrom" required>
                        <p>To:</p>      
                        <input type="number" name= "ato" required>
    </div>
    </li>
    <li>
        <button class="submit" type="submit">Submit Form</button>
    </li>
</ul>
    </div>

</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

问题是您在隐藏的输入字段上有required个属性。当提交表单时隐藏了这些字段(并且它们没有值),您将收到错误:

An invalid form control with name='from' is not focusable.
An invalid form control with name='to' is not focusable.
An invalid form control with name='dist' is not focusable.
An invalid form control with name='afrom' is not focusable.
An invalid form control with name='ato' is not focusable.

通过显示所有字段或删除required属性,表单提交正常。解决此问题的方法是在显示时添加required属性,并在隐藏时将其删除。

要实现这一点,首先要定义以下功能:

function setRequired()
{

  var r = document.getElementsByClassName('required')

  for (var i in r)
  {

    if (r[i].parentNode && r[i].parentNode.style.visibility == 'visible')
    {
      r[i].required = true
    }

    else
    {
      r[i].required = false
    }

  }

}

其次,将所有required属性替换为class="required"。第三,将;setRequired()添加到四个单选按钮的onclick个事件中(最后,setVisible之后)。

答案 1 :(得分:0)

变化:

<button class="submit" type="submit">Submit Form</button>

为:

<input type="submit">Submit Form</input>