简体   繁体   中英

How do I get the value of the checked radio button?

I have this simple script attached to a questionnaire and am having a problem getting the selected answer to show up in a textarea. Here is the script:

function check() {
  var complete = 0;
  var total = 0;

  for (i=0;i<document.form.length;i++)
  {
     if (document.form.elements[i].checked == true && complete < 10) {
        complete++;
        total = (total) + (Math.round(document.form.elements[i].value));
     }
  }

  if (complete >= 10) {
     document.form.message.value = document.form.question1.value;
  }
}

And here is the HTML:

<input type="radio" value="1" name="question1" onclick="check()"> A<br />
<input type="radio" value="2" name="question1" onclick="check()"> B<br />
<input type="radio" value="3" name="question1" onclick="check()"> C<br />
<input type="radio" value="4" name="question1" onclick="check()"> D<br />

<input type="radio" value="1" name="question2" onclick="check()"> E<br />
<input type="radio" value="2" name="question2" onclick="check()"> F<br />
<input type="radio" value="3" name="question2" onclick="check()"> G<br />
<input type="radio" value="4" name="question2" onclick="check()"> H<br />

<textarea name="message"></textarea>

I would like the value to be returned, but I am getting undefined . If I alter the line in the script that returns the text to:

  document.form.message.value = document.form.question1;

I get [object NodeList] . I know I am missing something so simple but for the life of me I cannot find it.

Also, is it possible I can return the letters A through H along with the value? I know I can replace the value with the letters but need the numbers there for calculations.

You are referencing a form element in your script, do you define a form?

The answer seems to be addressed here Attach event listener through javascript to radio button

My answer is going under the assumption that you would like the <textarea> to be populated with text similar to:

User answered 1 for Question A

User answered 2 for Question F

To get the A or F passed back, I needed to modify your html in the following way:

<input type="radio" value="1" name="question1" onclick="check(this, 'A')"> A<br />
<input type="radio" value="2" name="question1" onclick="check(this, 'B')"> B<br />
<input type="radio" value="3" name="question1" onclick="check(this, 'C')"> C<br />
<input type="radio" value="4" name="question1" onclick="check(this, 'D')"> D<br />

<input type="radio" value="1" name="question2" onclick="check(this, 'E')"> E<br />
<input type="radio" value="2" name="question2" onclick="check(this, 'F')"> F<br />
<input type="radio" value="3" name="question2" onclick="check(this, 'G')"> G<br />
<input type="radio" value="4" name="question2" onclick="check(this, 'H')"> H<br />

<textarea name="message"></textarea>

Otherwise, there's no actual connection between the letter and the radio input.

Anyway, here's what I done did:

I noticed that each group was repeating the same functionality, so I created a single Object Constructor:

var Answer = function () {
    this.text = '';
};

this.text will contain the special answer string per group.

Now let's create the two answer group objects:

var answerOne = new Answer();
var answerTwo = new Answer();

Next comes the check() function where we pass the input element as well as it's associated answer character:

var check = function (_input, _question) {
    if (_input.name === "question1") {
        answerOne.text = "User answered " + _input.value + " for Question " + _question + "\n";
    }
    if (_input.name === "question2") {
        answerTwo.text = "User answered " + _input.value + " for Question " + _question + "\n";
    }
    document.getElementsByName('message')[0].value = answerOne.text + answerTwo.text;
};

Now, as the user selects an answer, the appropriate answer group's string gets updated accordingly without affecting the other group's answer string.

Here's a jsfiddle with it working: http://jsfiddle.net/smokinjoe/uC76f/13/

Hope that helps!

Here you go the complete solution.

Couple of things went wrong in your code. 1. The way you get values from radio group. You need to iterate and find out which is checked 2. Setting value to textarea. You need to do getElemenetsByName[x]

<script>
function check() {
  var complete = 0;
  var total = 0;

    var x = document.getElementsByTagName('input');
    for(var k=0;k<x.length;k++){

            if (x[k].checked && complete < 10) {
                complete++;
                total = total + Math.round(x[k].value);
            } 

    }    

    (document.getElementsByName('message')[0]).value = total;       
}


</script>
<input type="radio" value="1" name="question1" onclick="check()"> A<br />
<input type="radio" value="2" name="question1" onclick="check()"> B<br />
<input type="radio" value="3" name="question1" onclick="check()"> C<br />
<input type="radio" value="4" name="question1" onclick="check()"> D<br />

<input type="radio" value="1" name="question2" onclick="check()"> E<br />
<input type="radio" value="2" name="question2" onclick="check()"> F<br />
<input type="radio" value="3" name="question2" onclick="check()"> G<br />
<input type="radio" value="4" name="question2" onclick="check()"> H<br />

<textarea name="message"></textarea>

Because it's a radio button, you need to loop through all values to find the one that has been selected. Something like this should work:

for (var i=0; i < document.form.question1.length; i++)
   {
   if (document.form.question1[i].checked)
      {
       document.form.message.value = document.form.question1[i].value;
      }
   }
}

Not tested this, and as I don't know the name (or id) of your form(s), or indeed how many forms you have in your document, I have referenced your form by it's id.

function check() {
    var complete = 0;
    var total = 0;
    var formId = 'EnterYourFormId';  // This could be passed as a paramter to the function instead (e.g. "function check(formId) {")
    var _from = document.getElementById(formId);  // The form could also be referenced by it's index, e.g. document.forms[0]

    for (i=0; i < _from.elements.length; i++) {
        if (_from.elements[i].type=='checkbox' && _from.elements[i].checked && complete < 10) {
            complete++;
            total = total + parseInt(_from.elements[i].value);
        }
     }

     if (complete >= 10) {
         _form.message.value = _form.question1.value;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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