0

我会很感激我能得到的所有帮助!

我正在使用 Happy.js 验证表单客户端

我要验证的第一个输入是带有是/否的单选按钮。此输入的 ID 与 javascript 函数隐藏/显示两个 div 相关联,因此我无法将 id 更改为与 happy.js 相同:

<input id="id_radio1" type="radio" name="name_radio1" value="Yes" aria-required="true" required/><label for ="id_radio1">Yes</label>
<input id="id_radio2" type="radio" name="name_radio2" value="No" aria-required="true" required//><label for ="id_radio2">No</label>

我的happy.js脚本如下,第一个字段#id_radio1是有问题的代码:

   <script>
      $(document).ready(function () {
        $('#awesomeForm').isHappy({
          fields: {
            // reference the field you're talking about, probably by `id`
            // but you could certainly do $('[name=name]') as well.
            '#id_radio1': {
              required: true,
              message: 'Please specify Gas Spring Type'
            },      
            '#qnty': {
              required: true,
              message: 'Please specify # of Units'
            },
            '#street': {
              required: true,
              message: 'Please enter your Street',
            },
             '#city': {
              required: true,
              message: 'Please enter your City',
             },
             '#state': {
              required: true,
              message: 'Please enter your State/Province',
            },
             '#country': {
              required: true,
              message: 'Please enter your Country',
            },
             '#zip': {
              required: true,
              message: 'Please enter your Zip/Postal Code',
            },
             '#email': {
              required: true,
              message: 'Please enter a valid Email Address',
              test: happy.email // this can be *any* function that returns true or false
            },
          }
        });
      }); 
    </script>

谢谢您的帮助。

供您参考,这是 DIV 开关的脚本:

<script language="javascript">
$(document).ready(function () {
         $('#div1,#div2').hide();
                    $('#id_radio1').click(function () {
                       $('#div2').hide('fast');
                       $('#div1').show('fast');
                });
                $('#id_radio2').click(function () {
                      $('#div1').hide('fast');
                      $('#div2').show('fast');
                 });
});</script>
4

2 回答 2

1

您将需要添加一个自定义函数来验证单选按钮并使用输入名称引用它们。像这样的东西应该工作:

$('#awesomeForm').isHappy({
      fields: {
          'input[name=name_radio1]':{
              required: 'sometimes',
              message: 'Please select a value',
              test: function(){ 
                  return $('input[name=name_radio1]').is(':checked');
              },
          }
      }
});
于 2013-08-14T02:09:21.440 回答
0

两个单选按钮的名称应该相同,ID 不能相同。

于 2013-08-13T20:26:16.193 回答