简体   繁体   English

JS函数检查所有复选框,不需要

[英]JS Function Checks all Checkboxes, which is not needed

Sorry that the title is very weird. 抱歉,标题很奇怪。 So, I am using Visual Studio MVC and I am creating a website. 因此,我正在使用Visual Studio MVC,并且正在创建一个网站。 I have 5 rows of 30 buttons in bootstrap. 我在引导程序中有30行按钮的5行。 When you click on these buttons a modal pops up with a checkbox. 当您单击这些按钮时,将弹出一个带有复选框的模态。 When the checkbox is clicked, it makes the modal button change to red. 单击复选框后,它会使模式按钮变为红色。 Unfortunately, it checks all the checkboxes for each button and each modal. 不幸的是,它检查了每个按钮和每个模式的所有复选框。 I have a feeling it's because of the id or something, but I cannot change each id for each button because I am creating over 150 buttons. 我感觉是因为id或其他原因,但是我无法更改每个按钮的每个id,因为我要创建150多个按钮。 So I need only 1 button to change colors when the checkbox is clicked. 因此,单击复选框时,我只需要一个按钮即可更改颜色。 I have added the jsfiddle, for a more simple view.Thank you for all your help. 我添加了jsfiddle,以获得更简单的视图。感谢您的所有帮助。


This is related to another post I created: Link 这与我创建的另一篇文章有​​关: 链接

JSFiddle: Fiddle Link JSFiddle: 小提琴链接

So far I have: 到目前为止,我有:

@{int k= 0;}
@for (int j = 0; j < 5; j++) { 
    @for (int i = 0; i < 30; i++)
    {
        k = k + 1;

    }
}

Inside the second for loop is: 在第二个for循环内是:

<button id="btn1" type="button" class="btn btn-primary btn-xs gridbtn" data-toggle="modal" data-target="#myModal">@k</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">
  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">@(k)</h4>
     </div>
     <div class="modal-body">
       <form role="form">
         <div class="checkbox callback-to-button">
           <label><input type="checkbox"> Remember me</label>
         </div>
         <button type="submit" class="btn btn-default" data-dismiss="modal">Submit</button>
       </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Since you are already looping, you can easily associate a dynamic id for each instance. 由于您已经在循环,因此可以轻松地为每个实例关联一个动态ID。

<button id="btn@k" type="button" class="btn btn-primary btn-xs gridbtn" data-toggle="modal" data-target="#myModal@k">@k</button>
<!-- Modal -->
<div class="modal fade" id="myModal@k" role="dialog">
  <div class="modal-dialog">
  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">@(k)</h4>
     </div>
     <div class="modal-body">
       <form role="form">
         <div class="checkbox callback-to-button">
           <label><input type="checkbox" data-target="#btn@k"> Remember me</label>
         </div>
         <button type="submit" class="btn btn-default" data-dismiss="modal">Submit</button>
       </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Having a event listener to the div is a bad idea, you can have a change event listener on the checkbox itself, and if you see clearly on the above HTML i have added data-target attribute to checkbox. 有一个div的事件侦听器是一个坏主意,您可以在复选框本身上有一个更改事件侦听器,如果您在上述HTML上清楚看到,我已经向复选框添加了data-target属性。

Please refer to the Javascript below: 请参考以下Javascript:

$('.modal input[type="checkbox"]').change(function(){
 var target = $(this).attr('data-target');

 $(target).css({'background':'red'});
});

I am sorry for all the confusion, but it was actually my modal not having a unique id and so the first modal of the for loop was created 150 times. 我为所有的混乱感到抱歉,但这实际上是我的模态没有唯一的ID,因此for循环的第一个模态被创建了150次。

I simply had to put @(k) in the modal id. 我只需要在模式ID中输入@(k)。

<div class="modal fade" id="myModal-@(k)" tabindex="-1" role="dialog">

Works amazing now! 现在效果惊人! This post helped: Link 这篇文章帮助: 链接

Thanks for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM