我有一些元素(输入、文本区域、控件组)和表单元素之外的提交按钮......
我可以验证这些元素(客户端)吗?例如,检查它们是否是必需的,如果它们为空则显示消息错误。我可以使用 jquery 验证插件来做到这一点吗?如何?
到目前为止,这是我的代码示例:
<div id="formContainer">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>User:</legend>
<div id="usersContainer">
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="1" />
<label for="radio-choice-1">User1</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="2" />
<label for="radio-choice-2">User2</label>
</div>
</fieldset>
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="subjectMessage">Subject:</label>
<input type="text" name="subjectMessage" id="subjectMessage" value="" placeholder="Subject" />
</div>
<div data-role="fieldcontain" class="ui-hide-label">
<label for="bodyMessage">Body:</label>
<textarea name="bodyMessage" id="bodyMessage" placeholder="Body" rows="8"></textarea>
</div>
<input id="sendMessageBtn" type="submit" value="Send" />
</div>
<script type="text/javascript">
$(document).one("pageinit", function () {
$("#sendMessageBtn").on("click", function () {
var userId= $('#formContainer').find("#usersContainer :radio:checked").val();
var subject = $('#formContainer').find("#subjectMessage").val();
var body = $('#formContainer').find("#bodyMessage").val();
//send data to the server...
});
});
</script>