将所有单选按钮的值打印到文本区域?

Printing values of all radio buttons to a textarea?

所以我在一个范围内有一个单选按钮列表,当按下编码时,我想使用 JavaScript 将这些单选按钮的值打印到文本区域(id 为:BINARYBit),使用函数 bin2dec。如果选择了收音机,我需要将这些值设置为 1,如果未选择,则需要这些值设置为 0。我在下面添加了一个片段,显示了我的整个文档。 有什么想法吗 ? 谢谢!

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <link rel="stylesheet" type="text/css" href="bootstrap.css" />

</head>

<body>

  <div class="container">

    <div class="col-lg-8">
      <label for="HEXBit">Input the Bitmask Here</label>
      <br/>
      <input type="text" class="form-control" id="DECBit" />
      <br/>
      <input type="submit" class="btn btn-primary btn-sm" id="Submit" value="Decode" onclick="dec2bin();" />
      <br/>
      <input type="text" class="form-control" id="BINARYBit" disabled="disabled" />
      <br/>
      <input type="submit" class="btn btn-primary btn-sm" id="SubmitCon" value="Encode" onclick="bin2dec();" />
      <br/>
      <span id="radiocheck">

<br/>
    <input type="radio" id="BinaryMessage" />
    <label for="BinaryMessage">Send Binary Message</label>

<br/>
    <input type="radio" id="Param1" />
    <label for="Param1">Param1</label>

<br/>
    <input type="radio" id="MDMID" />
    <label for="MDMID">Modem ID </label>

<br/>
    <input type="radio" id="GPIO" />
    <label for="GPIO">GPIO</label>

<br/>
    <input type="radio" id="AnalogDigi1" />
    <label for="AnalogDigi1">Digital/Analog 1</label>

<br/>
    <input type="radio" id="AnalogDigi2" />
    <label for="AnalogDigi2">Digital/Analog 2</label>

<br/>
    <input type="radio" id="StoreMsg" />
    <label for="StoreMsg">Save Message if no GPRS</label>

<br/>
    <input type="radio" id="InputNum" />
    <label for="InputNum">Input Event Number</label>

<br/>
    <input type="radio" id="GPSDate" />
    <label for="GPSDate">GPS Date</label>

<br/>
    <input type="radio" id="GPSStatus" />
    <label for="GPSStatus">GPS Status</label>

<br/>
    <input type="radio" id="GPSLat" />
    <label for="GPSLat">Latitude</label>

<br/>
    <input type="radio" id="GPSLong" />
    <label for="GPSLong">Longitude</label>

<br/>
    <input type="radio" id="GPSSpeed" />
    <label for="GPSSpeed">GPS Speed (Knots)</label>

<br/>
    <input type="radio" id="GPSHeading" />
    <label for="GPSHeading">Heading</label>

<br/>
    <input type="radio" id="GPSTime" />
    <label for="GPSTime">GPS Time</label>

<br/>
    <input type="radio" id="GPSAlt" />
    <label for="GPSAlt">Altitude</label>

<br/>
    <input type="radio" id="GPSNoSAT" />
    <label for="GPSNoSAT">Number of GPS Satelites</label>
    
<br/>
    <input type="radio" id="LowPowerMsg" />
    <label for="LowPowerMsg">Stop Messages In Low Power</label>

<br/>
    <input type="radio" id="SMSNoGPRS" />
    <label for="SMSNoGPRS">Send SMS When No GPRS</label>

<br/>
    <input type="radio" id="LastKnownGPS" />
    <label for="LastKnownGPS">Use Last Known GPS When Unavaliable</label>

<br/>
    <input type="radio" id="GPSOdo" />
    <label for="GPSOdo">Odometer</label>

<br/>
    <input type="radio" id="RTCTime" />
    <label for="RTCTime">RTC Time</label>

<br/>
    <input type="radio" id="ShortID" />
    <label for="ShortID">Use Short Modem ID</label>

<br/>
    <input type="radio" id="BattLVL" />
    <label for="BattLVL">Power Level</label>

<br/>
    <input type="radio" id="GPSOverSpeed" />
    <label for="GPSOverSpeed">GPS Overspeed Data</label>

<br/>
    <input type="radio" value="1" id="PCELL" />
    <label for="PCELL">PCELL Data</label>

<br/>
    <input type="radio" id="GPSALTOvr" />
    <label for="GPSALTOvr">GPS Alternative Over Speed</label>

</span>

    </div>

  </div>

</body>

<script language="JavaScript">
  function dec2bin() {
    var dec = document.getElementById("DECBit").value

    var val = (dec >>> 0).toString(2);

    var pad = "00000000000000000000000000000000";

    var answer = pad.substring(0, pad.length - val.length) + val;

    var arr = [];

    for (var i = 0; i < answer.length; i++) {

      arr[i] = (answer.charAt(i) == "1" ? true : false);
    }

    arr.reverse();

    console.log(answer);

    console.log(arr);

    document.getElementById("BINARYBit").value = answer;

    var span = document.getElementById("radiocheck");

    var inputs = span.getElementsByTagName("input");

    for (var i = 0; i < arr.length; ++i) {
      var thing = inputs[i];
      thing.checked = arr[i];

    }

    function bin2dec() {

    }
  }
</script>

</html>

使用本机 javascript,最好的方法是为所有要检查的输入提供 class,例如 class="item",然后在函数如下:

function bin2dec() {
    var printableEncoded = "",
        items = document.getElementsByClassName("item");

    // For each item if it's checked include a '1', else a '0'
    for (var i = 0; i < items.length; i++) {
        printableEncoded += items[i].checked ? "1" : "0";
    }

    // Change the value of the disabled input to the encoded string
    document.getElementById("BINARYBit").value = printableEncoded;
}

HTML 示例:

<input type="radio" value="1" id="PCELL" class="item" />
<label for="PCELL">PCELL Data</label>

解决方案使用 jquery

    function encode()
  {
     var value="";
      //select all  radio buttons
      $(".container input[type='radio']").filter(function(i,el){

        //check if radio button is checked
        value=value + ( ($(el).prop("checked")==true)? "1":"0");
      });

    $("#BINARYBit").val(value);

  }

参考: