c# - 强制用户在发布前至少点击一次按钮
问题描述
我希望用户在发布表单信息之前单击一个按钮以在我的页面上添加一行。
基本上页面上有一个按钮,可以将字符串行添加到 div,我想确保他们至少单击了一次此按钮。如果他们有,他们可以成功发布,如果没有,我想显示一条错误消息,说明 div 必须至少有 1 个项目
我不知道如何解决这个问题。我应该使用jQuery
吗?有没有办法做到这一点DataAnnotations
解决方案
jQuery 似乎很适合这个:
/* Lets declare a global boolean variable to hold the status of having at least one item in the div */
var rowAdded = false;
// listen for the add-row click. When clicked, toggle rowAdded.
$('button').click(function() {
rowAdded = true;
});
// listen for the form submission and check rowAdded before sending the form.
$('#submit').click(function() {
if(rowAdded){
// submit the form
} else {
// user has not added a string row to the div, prompt them to add a row.
// prompt method 1, use alert()
alert('Please add a row before submitting this form.');
// prompt method 2, print string to the dom
var promptString = 'Please add a row before submitting this form.';
$('#prompt-div').append('<p>' + promptString + '</p>');
}
});
推荐阅读
- python - 为什么在类中调用另一个函数时会收到“名称未定义”错误消息?
- c++ - 结构的 std::vector:调整矢量大小后结构成员的初始值是多少?
- formatting - man – 我怎样才能使单词斜体的一部分?
- java - 3的中位数比java中通常的快速排序花费更多时间
- java - 如何在 JAX-RS 中使数据字段成为可选(非必需)?
- github - 如何强制工程师使用同步的 fork 分支
- python - 具有多种状态的 Pygame 动画
- spring - Spring Cloud数据流如何实现多租户数据库
- javascript - 文本对齐“中心”不适用于表头
- node.js - 有没有办法在 Dialogflow 中检索对话历史记录?