douni1396 2016-03-09 23:29
浏览 83
已采纳

Javascript / HTML - 将函数返回变量传递给隐藏的输入值

I've read heaps of posts about this and I'm still having issues with passing the return variable from my function to a hidden input value. The function is checking all radio buttons on the page (12 of them) and finding the one which is selected. It is either passing NULL or the javascript to screen.php. I have tested the checked() function and it works (used alert() to show the value). I've read heaps of posts on passing values to hidden inputs and none of those solutions have worked for me.

If you need to see more code, I can post it on pastebin.

<script language="text/javascript">
    function checked()
    {
        var inputs = document.getElementsByClassName('radio');
        var valueSelected;
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].checked) valueSelected = inputs[i].value;
        }

        return valueSelected;
    }
    </script>
</head>

<body>
    <div id="header-wrapper">
        <div id="header">
            <div id="logo">
                <h1><a href="index.php">KSM</a></h1>
            </div>
        </div>
        <div id="buttons">
        <ul>
        <li>
        <form id="form_ready" method="post" action="../backend/screen.php">
            <input type="hidden" name="screenid" value="1" />
            <input type="hidden" name="status" value="READY" />
            <input type="hidden" name="pickupid" id="pickupid" value="" />
            <input type="submit" class="submit" name="READY" onclick="document.getElementById('pickupid').value = checked();">
        </form>
        <div id="search">
            <ul id="nav">
                <li class="tab1"><a href="index_tbl.php">Ordered</a></li>
            </ul>
        </div>
        <div id="content">
            <div id="container">
            <table>
                <tr>
                <td>
                    <input type="radio" class="radio" name="pickup" id="1" value="001" />
                        <label for="1"> <span>001</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="2" value="002" />
                        <label for="2"> <span>002</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="3" value="003" />
                        <label for="3"> <span>003</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="4" value="004" />
                        <label for="4"> <span>004</span> </label>
                </td>
                </tr>
                <tr>
                <td>
                    <input type="radio" class="radio" name="pickup" id="5" value="005" />
                        <label for="5"> <span>005</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="6" value="006" />
                        <label for="6"> <span>006</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="7" value="007" />
                        <label for="7"> <span>007</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="8" value="008" />
                        <label for="8"> <span>008</span> </label>
                </td>
                </tr>
                <tr>
                <td>
                    <input type="radio" class="radio" name="pickup" id="9" value="009" />
                        <label for="9"> <span>009</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="10" value="010" />
                        <label for="10"> <span>010</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="11" value="011" />
                        <label for="11"> <span>011</span> </label>
                </td>
                <td>
                    <input type="radio" class="radio" name="pickup" id="12" value="012" />
                        <label for="12"> <span>012</span> </label>
                </td>
                </tr>
            </table>
        </div>
    </div>
</body>

  • 写回答

1条回答 默认 最新

  • dongxian8048 2016-03-10 02:42
    关注

    What's going on with the current code you have is you're submitting the form before the JS has time to execute your function and put the data into the hidden input. I wouldn't really suggest doing it in the way you're doing now, but here's a quick fix.

    In your HTML change the form to this

        <form id="form_ready" method="post" action="../backend/screen.php" onsubmit="return checked()">
    

    And in your JS change your checked function to:

    function checked(){
        var inputs = document.getElementsByClassName('radio');
        var valueSelected;
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].checked) valueSelected = inputs[i].value;
        }
    
        return (document.getElementById('pickupid').value = valueSelected);
    }
    

    Also, get rid of the onclick on that submit button.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?