Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如果在ASP.NET mvc中选中复选框,则使按钮处于启用状态_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 如果在ASP.NET mvc中选中复选框,则使按钮处于启用状态

Asp.net mvc 如果在ASP.NET mvc中选中复选框,则使按钮处于启用状态,asp.net-mvc,Asp.net Mvc,在我的项目中,我在for循环中创建了几个复选框: for(int i=0; i<10; i++) { {<input type="checkbox" name="check"/> @i<br>} } 对于(int i=0;i您必须启动一个事件 比如: <input type="submit" name="DelButton" class="inputButton" id="DelButton" value=" DelButton" /&g

在我的项目中,我在for循环中创建了几个复选框:

for(int i=0; i<10; i++)
{
        {<input type="checkbox" name="check"/> @i<br>}
}

对于(int i=0;i您必须启动一个事件

比如:

  <input type="submit" name="DelButton" class="inputButton" id="DelButton" value=" DelButton" />

  <input type="checkbox"  onchange="document.getElementById('DelButton').disabled = !this.checked;" />

您可以对
JQuery
执行以下操作:

  $('.checkbox_class').change(function() {
    if ($('.checkbox_class:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
if ($('input[type="checkbox"]:checked').length > 0) {
    // at least one is checked
}
$('#DelButton').prop('disabled', false);
$('input[type="checkbox"]').change(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
Html代码:

   <input type="checkbox" class="checkbox_class" />
<input type="checkbox" class="checkbox_class" />


<input type="button" id="DelButton" disabled  value="Delete" />

我希望能帮助你


这里的实时演示:

虽然您没有明确提到jQuery的使用,但它默认情况下是与ASP.NET MVC一起提供的

首先,响应复选框的更改事件:

$('input[type="checkbox"]').change(function() {
    // respond to change
});
这将在页面上的任何复选框更改其状态时调用。然后在此事件处理程序中,您只想知道是否选中了任何复选框,对吗?为此,您可以选择所有选中的复选框,然后查看是否有。类似于以下内容:

  $('.checkbox_class').change(function() {
    if ($('.checkbox_class:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
if ($('input[type="checkbox"]:checked').length > 0) {
    // at least one is checked
}
$('#DelButton').prop('disabled', false);
$('input[type="checkbox"]').change(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
然后,在if/else结构中,您只需要启用/禁用该按钮。该按钮如下所示:

  $('.checkbox_class').change(function() {
    if ($('.checkbox_class:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
if ($('input[type="checkbox"]:checked').length > 0) {
    // at least one is checked
}
$('#DelButton').prop('disabled', false);
$('input[type="checkbox"]').change(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
把这些放在一起,你可能会有这样的情况:

  $('.checkbox_class').change(function() {
    if ($('.checkbox_class:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});
if ($('input[type="checkbox"]:checked').length > 0) {
    // at least one is checked
}
$('#DelButton').prop('disabled', false);
$('input[type="checkbox"]').change(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        $('#DelButton').prop('disabled', false);
    } else {
        $('#DelButton').prop('disabled', true);
    }
});

你试过自己做吗?@Andrei我想人们在不知道如何实现自我的时候会问问题,否则问问题有什么意义,如果你能自己做的话。问题说明有多个复选框,而不仅仅是一个。问题
一个或多个复选框被选中。
是的,这个词“更多”意味着“不止一个”。因此,使用
id
将不起作用。这个逻辑如何处理多个复选框?(提示:它将仅基于最近单击的复选框启用/禁用按钮,而不是基于所有复选框的当前状态。)@David我将答案更新为多个复选框,您更新的示例将不再工作。在JSFIDLE中,选择两个复选框,然后取消选择其中一个。按钮会发生什么情况?当有多个复选框时,这将如何表现?@David如果有多个复选框,最好使用其他方法,如解决方案您建议,但用户仅指定他希望使用单个复选框禁用其删除底部“
”,但用户仅指定他希望使用单个复选框禁用其删除底部“-您可能想再看看这个问题。特别是“一个或多个复选框”的措辞“也特别是创建复选框的循环。@David“一个或多个…”表示一个或多个复选框,可以是一个,也可以是多个……。问题明确定义了多个复选框。请看问题开头的循环。在什么条件下,该循环只会发出一个复选框?(提示:它将始终恰好发出10个复选框。)