简体   繁体   English

从复选框添加/删除值

[英]Add/Remove values from checkbox

So I check normalize to add normalize library... 所以我检查规范化添加规范化库...

<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />

I check jQuery and I add the jQuery source after normalize... 我检查jQuery,并在规范化后添加jQuery源。

<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

but if I uncheck normalize, I want it to remove the normalize link, and if I check it again I want to add normalize after jQuery... 但是如果我取消选中规范化,我希望它删除规范化链接,如果再次进行检查,我想在jQuery之后添加规范化...

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />

That's what's suppose to happen but instead when I add say AngularJS, and then normalize, it's suppose to show like this... 那是应该发生的事情,但是当我添加说AngularJS然后进行规范化时,它应该像这样显示...

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" />
<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />

but instead I get.... 但是我得到了...

<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" />

So basically what I check I want that library to add after the other libraries I check. 所以基本上我要检查的东西是我要在我检查的其他库之后添加该库。

My jQuery is... 我的jQuery是...

// Add/Remove Library from Checkbox
$(".check").on("change", function() {

  // Join All Checked Libraries
  $(".full-library-code").val(function() {
    return $.map($(".check:checked").next().next(), function (el) {
      return el.value;
    }).join('\n');
  });
});

I believe this can happen without Codemirror , and I believe this problem is happening because of how I have my html structured with my jQuery, but I have no idea how to solve it. 我相信如果没有Codemirror会发生这种情况,并且我相信会发生此问题是因为我用jQuery构造HTML的方式,但我不知道如何解决。

 $(document).ready(function() { // Add/Remove Library from Checkbox $(".check").on("change", function() { // Join All Checked Libraries $(".full-library-code").val(function() { return $.map($(".check:checked").next().next(), function (el) { return el.value; }).join('\\n'); }); }); }); 
 <link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" /> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <input type="checkbox" class="check" id="norm"> <label for="norm">Normalize</label> <input type="text" class="hide" value='<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />' /><br> <input type="checkbox" class="check" id="jquery"> <label for="jquery">JQuery</label> <input type="text" class="hide" value='<script src="http://code.jquery.com/jquery-latest.min.js"></script>' /><br> <input type="checkbox" class="check" id="ang"> <label for="ang">Angular JS</label> <input type="text" class="hide" value='<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" />' /><br> <textarea class="full-library-code" placeholder="full library's code"></textarea> 

Instead of looping all the checkboxes every time you should look if the clicked checkbox is 'checked' or 'unchecked'. 不必每次都循环查看所有复选框,而应查看所单击的复选框是“选中”还是“未选中”。 Based on that, add or remove the text. 基于此,添加或删除文本。

 $(document).ready(function() { $(".check").on("change", function() { var textarea = $('.full-library-code'); var value = $(this).nextAll('input:first').val() + '\\n'; if( $(this).prop('checked') == true ) textarea.val( textarea.val() + value ); else textarea.val( textarea.val().replace( value, "") ); }); }); 
 /* only for demo readability */ textarea { width: 500px; height: 200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="check" id="norm" /> <label for="norm">Normalize</label> <input type="text" class="hide" value='&lt;link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" /&gt;' /><br /> <input type="checkbox" class="check" id="jquery" /> <label for="jquery">JQuery</label> <input type="text" class="hide" value='&lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;' /><br /> <input type="checkbox" class="check" id="ang" /> <label for="ang">Angular JS</label> <input type="text" class="hide" value='&lt;link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" /&gt;' /><br /> <textarea class="full-library-code" placeholder="full library's code"></textarea> 

Note: please mind A. Wolff's comment, you probably want to add AngularJS as a script... also, in the values I replaced the < and > for &lt; 注意:请介意A. Wolff的评论,您可能想将AngularJS添加为脚本...而且,在将<>替换为&lt;的值中&lt; and &gt; &gt;

If you're getting the element in reverse order, simply use the reverse() method before the join() , like so: 如果要以相反的顺序获取元素,只需在join()之前使用reverse()方法,如下所示:

// Join All Checked Libraries
$(".full-library-code").val(function() {
  return $.map($(".check:checked").next().next(), function (el) {
     return el.value;
  }).reverse().join('\n');
});

What I understand from your question is that you want the later checked libraries to appear later in the textarea. 我从您的问题中了解到的是,您希望稍后检查的库在文本区域中稍后出现。 Here is how you can do that 这是你可以做到的

 $(".check").on("change", function() { var $textArea = $(".full-library-code"); var fullLibraryCode = $textArea.val(); var thisValue = $(this).next().next().val() + "\\n"; if (this.checked) { $textArea.val(fullLibraryCode + thisValue); } else { $textArea.val(fullLibraryCode.replace(thisValue, "")); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="check" id="norm"> <label for="norm">Normalize</label> <input type="text" class="hide" value='<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />' /> <br> <input type="checkbox" class="check" id="jquery"> <label for="jquery">JQuery</label> <input type="text" class="hide" value='<script src="http://code.jquery.com/jquery-latest.min.js"></script>' /> <br> <input type="checkbox" class="check" id="ang"> <label for="ang">Angular JS</label> <input type="text" class="hide" value='<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" />' /> <br> <textarea class="full-library-code" placeholder="full library's code"></textarea> 

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

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