社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

如何从复选框jquery中收集选中的值

turtlefish12 • 7 年前 • 2312 次点击  

我正在收集特定复选框的值并将其附加到另一个列表中。

这是我HTML文件的一部分。

    <fieldset>
        <legend>Pick the foods you like.</legend>

        <input id="tacos" type="checkbox" name="food" value="tacos">
        <label for="tacos">Tacos</label>

        <input id="crepes" type="checkbox" name="food" value="crepes">
        <label for="crepes">Crepes</label>

        <input id="dumplings" type="checkbox" name="food" value="dumplings">
        <label for="dumplings">Dumplings</label>

    </fieldset>

下面是我目前使用的javascript。

var $food = $('[name="food"]:checked');
$li.append($food);

首先,我得不到所有的食物价值。第二,我实际上只是附加复选框,然后从字段集中删除复选框。我理解 $food 应该是个单子,但我不太清楚怎么写。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43476
文章 [ 4 ]  |  最新文章 7 年前
Mr Lister Paul Sweatte
Reply   •   1 楼
Mr Lister Paul Sweatte    7 年前

获取带有食品名称的检查项目,然后将其附加到 <ul>

function submit(){
var $food = $('[name="food"]:checked');
var $ul= $('#li-id');
var li='';
$food.each(function () {
    li += '<li>' + this.value + '</li>';
});

 $ul.html(li);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset>
        <legend>Pick the foods you like.</legend>

        <input id="tacos" type="checkbox" name="food" value="tacos">
        <label for="tacos">Tacos</label>

        <input id="crepes" type="checkbox" name="food" value="crepes">
        <label for="crepes">Crepes</label>

        <input id="dumplings" type="checkbox" name="food" value="dumplings">
        <label for="dumplings">Dumplings</label>

    </fieldset>
    <ul id='li-id'>
    
    </ul>
              <button type="button" class="btn btn-primary" onclick="submit()">submit</button>
gavgrif
Reply   •   2 楼
gavgrif    7 年前

您可以根据所选元素生成选项的动态列表,并在字段集的点击上触发。

$('fieldset').click(function(){
  var food = document.querySelectorAll('[name="food"]:checked');
  var foodStr = ''; 
  food.forEach(function(item){
    foodStr += '<li>' + item.value + '</li>';
  })
  $('#selectedItems').html(foodStr);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset>
        <legend>Pick the foods you like.</legend>

        <input id="tacos" type="checkbox" name="food" value="tacos">
        <label for="tacos">Tacos</label>

        <input id="crepes" type="checkbox" name="food" value="crepes">
        <label for="crepes">Crepes</label>

        <input id="dumplings" type="checkbox" name="food" value="dumplings">
        <label for="dumplings">Dumplings</label>

    </fieldset>
    <br/>
    <ul id="selectedItems"></ul>
Sagar
Reply   •   3 楼
Sagar    7 年前

你必须遍历所有的复选框

var $food = $('[name="food"]:checked');
$food.each(function () {
   $li.append(this.value);
});
Oliver Trampleasure
Reply   •   4 楼
Oliver Trampleasure    7 年前

您当前的问题是,作为一个整体,您正在选择复选框本身。您只想选择标签的值,而不想移动标签,而是将其复制到其他位置。

下面的代码使用jquery,该jquery在单击带有 id #check . 代码在每个复选框中循环 name=food 已检查,并附加 label 链接到列表的复选框的文本。 更新: 然后将此列表附加到一系列列表中,并根据请求添加一个标题。

我已经包含了一条线,如果你想在每个检查之间重置列表,你可以取消评论(否则你可以在列表中有相同版本的食物)。

如果你还需要什么,请告诉我。 希望有帮助

$("#check").click(function() {

  // Uncomment the line below if you want to reset the list
  // $("#lists .favourite-list").remove();

  // Create variable to store list of foods if selected
  tempList = ""

  // Check if any foods are selected
  $('[name="food"]:checked').each(function() {

    // Get the text value of the label
    // Should do this, rather than use 'for' value as it might differ
    checkLabel = $("label[for='" + $(this).attr("value") + "']").text();

    // Append a new list element
    tempList = tempList + "<li>" + checkLabel + "</li>";

  })

  // Check if at least one food has been added
  if (tempList != "") {

    // Add ul wrappers to list and header.
    tempList = "<div class='favourite-list'><p>Favourite Foods:</p><ul>" + tempList + "</ul></div>"

    // Append new list
    $("#lists").append(tempList);

  }

});
#check {
  margin-top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset>
  <legend>Pick the foods you like.</legend>

  <input id="tacos" type="checkbox" name="food" value="tacos">
  <label for="tacos">Tacos</label>

  <input id="crepes" type="checkbox" name="food" value="crepes">
  <label for="crepes">Crepes</label>

  <input id="dumplings" type="checkbox" name="food" value="dumplings">
  <label for="dumplings">Dumplings</label>

</fieldset>

<button id="check">Add to list</button>

<div id="lists">

</div>