duanchongzi9997 2013-08-19 17:50 采纳率: 0%
浏览 31

如何在提交前显示复选框值或在勾选时自己显示? [关闭]

I have the following codes:

<form method="post">
<input type="checkbox" name="fruit[]" value="apple" id="apple" /><label   for="apple">Apple</label><br />
<input type="checkbox" name="fruit[]" value="pinapple" id="pinapple" /><label for="pinapple">Pinapple</label><br />
<input type="checkbox" name="fruit[]" value="grapefruit" id="grapefruit" /><label for="grapefruit">Grapefruit</label><br />
<input type="submit" name="go" />
</form>
<?php 
$fruitList = implode(', ', $_POST['fruit']);
echo $fruitList;
?>

It will show the checked items after submit. Is this possible show the ticked items values inside a input box before submitting.

  • 写回答

3条回答 默认 最新

  • douciwang6819 2013-08-19 17:55
    关注

    Yes, it certainly is but it will take some javascript code.

    Here is an example based on the code snippet you pasted:

    http://jsfiddle.net/EmNSe/

    <form method="post" name="myForm">
        <input type="checkbox" name="fruit[]" value="apple" id="apple" onclick="selectionListener();" />
        <label for="apple">Apple</label>
        <br />
        <input type="checkbox" name="fruit[]" value="pinapple" id="pinapple" onclick="selectionListener();" />
        <label for="pinapple">Pinapple</label>
        <br />
        <input type="checkbox" name="fruit[]" value="grapefruit" id="grapefruit" onclick="selectionListener();" />
        <label for="grapefruit">Grapefruit</label>
        <br />
        <br />
        <label for="SelectionMade">Selection Made:</label>
        <textarea rows="4" cols="50" name-"SelectionMade" id="SelectionMade">
        </textarea>
        <br />
        <br />
        <input type="submit" name="go" />
    </form>
    

    and the javascript:

    function selectionListener() {
    
        checkedStr = '';
    
        if (document.getElementById('grapefruit').checked) {
            checkedStr = checkedStr + "grapefruit is checked!
    ";
        }
    
        if (document.getElementById('pinapple').checked) {
            checkedStr = checkedStr + "pinapple is checked!
    ";
        }
        if (document.getElementById('apple').checked) {
            checkedStr = checkedStr + "apple is checked!
    ";
        }
    
        document.getElementById('SelectionMade').value = checkedStr;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line