简体   繁体   English

HTML格式-将值传递到同一页面上的两种不同形式

[英]HTML FORMS - Passing values to two different forms on the same page

I am having problems trying to pass values to two different forms on the same page. 我在尝试将值传递到同一页面上的两种不同形式时遇到问题。 The page is populated with 16 radio buttons (which I've turned into boxes for display reasons) and they all hold a value (eg 001). 该页面上装有16个单选按钮(出于显示原因,我将其变成了方框),它们都包含一个值(例如001)。 Both of the forms jobs are to somehow grab the active/selected radio button value and post it to a PHP file so database changes can be made. 这两个表单作业都将以某种方式获取活动/选定的单选按钮值并将其发布到PHP文件,以便可以进行数据库更改。 Instead of a submit button, I am using an anchor to pass a JavaScript submit function. 我使用锚来传递JavaScript提交功能,而不是使用提交按钮。 I have tried having both forms cover the whole page but still didn't have any luck. 我尝试过两种形式都可以覆盖整个页面,但是仍然没有运气。

I'll post some code below to help you understand. 我将在下面发布一些代码以帮助您理解。

PS. PS。 If you need more code to understand, I can post it to pastebin. 如果您需要更多代码来理解,可以将其发布到pastebin。

<li>
<form id="form_ready" method="post" action="../backend/screen.php">
    <input type="hidden" name="screenid" value="" />
    <input type="hidden" name="status" value="" />
    <input type="hidden" name="pickupid" value="document.activeElement.value;" />
    <a onclick="document.getElementById('form_ready').submit();">READY</a>
</form>
</li>
<li>
<form id="form_c" method="post" action="../backend/screen.php">
    <input type="hidden" name="status" value="" /> 
    <input type="hidden" name="pickupid" value="document.activeElement.value;" />
    <a onclick="document.getElementById('form_c').submit();">COLLECTED</a>
</form>
</li>
</ul>

<div id="content">
<div id="container">
<div id="table">
    <div id="tr1">
    <div id="td1">
        <input type="radio" name="pickup" id="1" value="001" />
        <label for="1"> <span>001</span> </label>
    </div>
    <div id="td2">
        <input type="radio" name="pickup" id="2" value="002" />
        <label for="2"> <span>002</span> </label>
    </div>
    <div id="td3">
        <input type="radio" name="pickup" id="3" value="003" />
        <label for="3"> <span>003</span> </label>
    </div>
    <div id="td4">
        <input type="radio" name="pickup" id="4" value="004" />
        <label for="4"> <span>004</span> </label>
    </div>

To answer the suggestion in the comments and use only one form, here's how to grab the value from the selected radio button: 要回答评论中的建议并仅使用一种形式,以下是从所选单选按钮获取值的方法:

var inputs = document.getElementsByTagName('input');
var valueSelected;
for (var i = 0; i < inputs.length; i++) {
  if (inputs[i].checked) valueSelected = inputs[i].value;
}

valueSelected will contain the value of the selected radio. valueSelected将包含所选单选的值。

If you ever need to reduce the type to "radio" only for some reason, you can check an input type with inputs[i]['type'] === 'radio' in the for loop. 如果仅出于某种原因需要将类型减少为“ radio”,则可以在for循环中使用inputs[i]['type'] === 'radio'检查输入类型。


The best would probably still be to set a class for your "radio" inputs, it will allow the loop to be more specific, hence a more efficient code, for example: 最好的办法仍然是为您的“无线电”输入设置一个类,这将使循环更具体,从而使代码更有效,例如:

<input type="radio" class="myRadio">

and in JS: var inputs = document.getElementsByClassName('myRadio'); 在JS中: var inputs = document.getElementsByClassName('myRadio');

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

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