【问题标题】:Force user to click button at least once before posting强制用户在发布前至少点击一次按钮
【发布时间】:2019-08-20 16:48:53
【问题描述】:

我希望用户在发布表单信息之前单击一个按钮以在我的页面上添加一行。

页面上基本上有一个按钮,可以将字符串行添加到 div,我想确保他们至少单击了一次此按钮。如果他们有,他们可以成功发布,如果没有,我想显示一条错误消息,说明 div 必须至少有 1 个项目

我不确定如何解决这个问题。我应该使用jQuery 吗? DataAnnotations有没有办法做到这一点

【问题讨论】:

  • 您应该先尝试实现一些东西,如果您遇到困难,请给我们一些代码示例,或者问题将因过于宽泛或基于意见而被关闭。

标签: c# jquery razor model-view-controller


【解决方案1】:

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>');
    }
});

【讨论】:

    【解决方案2】:

    用户会知道该怎么做吗?

    你能不能只添加一个复选框来指示:[] 选中它来添加一行,然后是一个按钮来提交?

    【讨论】:

    • 不,不能使用复选框。遗憾的是,用户必须添加一行带有按钮的行。
    【解决方案3】:

    您可以简单地隐藏提交按钮(或禁用它)并仅显示用于添加该行的按钮,并在单击时显示提交按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多