如果一个/两个字段为空或一个/两个字段包含默认值,当用户尝试提交我的表单时,如何返回带有错误消息的警报对话框?
到目前为止,这是我的代码:
HTML
<!-- idea title -->
<textarea id="ideaTitle" />Please give your idea a title...</textarea>
<br />
<!-- idea body -->
<textarea id="ideaBody" rows="5" />Please provide details of your idea...</textarea>
<br />
<!-- submit button -->
<input type="button" id="sendMessage" value="broadcast" />
JS
$(function () {
$('#ideaTitle, #ideaBody').each(function () {
$.data(this, 'default', this.value);
}).focus(function () {
if (!$.data(this, 'edited')) {
this.value = "";
}
}).change(function () {
$.data(this, 'edited', this.value != "");
}).blur(function () {
if (!$.data(this, 'edited')) {
this.value = $.data(this, 'default');
}
});
});
演示:http: //jsfiddle.net/EKmmq/