将复选框值添加到div内容中

时间:2015-08-21 18:52:59

标签: jquery text checkbox

我在这里找到了这个代码...如何在警告上显示值,而不是像文本内容那样在div上显示它们?

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("button").click(function(){
        var favorite = [];
        $.each($("input[name='sport']:checked"), function(){            
            favorite.push($(this).val());
        });
        alert("My favourite sports are: " + favorite.join(", "));
    });
});

我想在div&#34;复选框值&#34;上显示值,我该怎么做?

<div id="checkboxvalues"></div>
<form>
  <h3>Select your favorite sports:</h3>
    <label><input type="checkbox" value="football" name="sport"> Football</label>
    <label><input type="checkbox" value="baseball" name="sport"> Baseball</label>
    <label><input type="checkbox" value="cricket" name="sport"> Cricket</label>
    <label><input type="checkbox" value="boxing" name="sport"> Boxing</label>
    <label><input type="checkbox" value="racing" name="sport"> Racing</label>
    <label><input type="checkbox" value="swimming" name="sport"> Swimming</label>
    <br>
    <button type="button">Get Values</button>
</form>

2 个答案:

答案 0 :(得分:1)

$("#checkboxvalues").html('My favourite sports are: ' + favorite.join(", "));

答案 1 :(得分:0)

您可以按照以下方式简化代码

holder.sAttedenceList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                if (!parent.hasFocus()) {
                    return;
                }
$(document).ready(function() {
  $("button").click(function() {
    $('#display').html($("input[name='sport']:checked").map(function() { return this.value; }).get().join());
  });
});