2013-12-10 29 views
0

我在運行時創建動態複選框。我的要求是必須驗證至少一個複選框是否被選中。我正在用springJS做這個。但爲了驗證我必須通過checkBox Id來驗證彈簧,但是這個ID數組在運行時創建。我怎麼能做到這一點?我嘗試了所有的解決方案,但它並沒有爲我工作。我這樣做是因爲它工作,如果我硬編碼複選框ID。使用spring JS的多個CheckBox驗證?

<script type="text/javascript"> 
    Spring.addDecoration(
     new Spring.ElementDecoration({ 
      elementId: '_CheckBox_ids', 
      widgetType: 'dijit.form.CheckBox', 
      widgetModule: 'dijit.form.CheckBox', 
      validate: function() { 
       if (dojo.query("#roo_apiUser_profile > input[type=checkbox]", 'dijit.form').filter(function (n) { 
        return n.checked; 
       }).length > 0) { 
        return true; 
       } else { 
        alert('choose at least one profile'); 
        return false; 
       } 
      }, 
      widgetAttrs: { 
       required: true 
      } 
     })); 
</script> 

回答

0

你可以在你form標籤添加onsubmit屬性和調用,它的複選框的驗證功能。

或者,您也可以使用dojo.declare創建自己的AbstractElementDecoration延長(在spring.js定義),並傳遞給validate屬性的功能,將做複選框驗證你。對於elementId,您可以傳入包含所有複選框的div元素的idSpring.ValidateAllDecoration調用Spring.validateAll函數。確保你在AbstractElementDecoration分機上做了必要的調整,以便在Spring.validateAll中沒有例外。