使用javascript匹配表单条目?

时间:2009-07-30 14:36:30

标签: javascript forms validation client-side

我有一个带有这样的标记的表单....

你裁掉哪张优惠券

<div class="answer item1">
  <input type="checkbox" name="downloaded_vouchers[answer1]" id="answer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>
<div class="answer item1">
   <input type="checkbox" name="downloaded_vouchers[answer1]" id="answer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>

您使用了哪个优惠券

 <div class="answer item1">
  <input type="checkbox" name="used_vouchers[answer1]" id="usedanswer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>
<div class="answer item1">
   <input type="checkbox" name="used_vouchers[answer1]" id="usedanswer1" value="1"/>
   <label for="answer1">Answer 1</label>
</div>

我需要能够以某种方式,在表单检查表明凭证剪切与使用的那个匹配,有一些验证服务器端但我需要做一些客户端,并且不知道在哪里开始,我需要使用直接javascript没有jquery或类似的,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

像这样......

   <script type="text/javascript">
    function validate( )
    {
        // use document.getElementById to get the form item
        var item = document.getElementById("yourElement");
        if ( somecondition == true )
        {
            alert("good to go");
            // allow the form to post
            return true;
        }
        else
        {
            alert("I don't think so");
            // return false so the form will not post
            return false;
        }
    }
    </script>

    <form onsubmit="Validate();" >
    </form>

从onsubmit方法返回true将发送表单,返回false将不会