javascript - 如何动态更改单选按钮显示的信息? [英] javascript - How to dynamically change information displayed by radio button?

查看:153
本文介绍了javascript - 如何动态更改单选按钮显示的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(重复?)我已经尝试了几个与此相关的Stackoverflow帖子,但是我无法获得一个javaScript示例。我希望暂时不要使用jQuery

(Duplicate?) I've tried several Stackoverflow postings related to this, but I cannot get a javaScript example to work. I'd like to avoid having to use jQuery, for the time being.

我想创建无线电显示的信息按钮动态,使用 javascript 。在这个例子中,我想编写一个函数,显示这些单选按钮答案1和答案2的其他值。例如,我实际上并不想要'答案1'。目标是让用户点击其中一个选择的答案,然后点击提交/保存自我检查自己的知识。

I want to create the information shown by radio buttons dynamically, using javascript. In this example, I would want to write a function that displays some other values for these radio buttons 'Answer 1' and 'Answer 2'. For example, I don't actually want 'Answer 1'. Goal is for the user to click on one of the multiple choice answers, then hit submit/save to self-check their own knowledge.

我已经学到了,通过我更复杂的项目代码,将html < form> 部分中的硬编码的提交/保存按钮似乎与单选按钮显示的值相关联,我设法添加使用javaScript *在我看来,通过单选按钮可能改变已经显示的

I have already learned, through my more complex project code, that a submit/save button that is hard-coded into the html <form> section does not seem to associate with values displayed by the radio buttons, that I managed to add in using javaScript * It seems to me that changing hardcoded information already displayed by the radio buttons might work.

当用户点击提交/保存按钮时,我不要需要参考实际的答案信息,单选按钮正在显示。我只需要知道在这种情况下,这是第一个还是第二个答案。

When user clicks on the submit/'save' button, I don't need to refer to the actual answer information that the radio button is displaying. I only need to know whether , in this case, it's the first or second answer chosen.

<html>

<script type="text/javascript">
function OnSubmitForm()

{
  if(document.myform.operation[0].checked == true)
    {
    alert ( "You have selected the first answer" );  
    }
  else
    if(document.myform.operation[1].checked == true)
        {
        alert ( "You have selected the SECOND answer" );  
        }

}
</script>
<form name="myform" onsubmit="return OnSubmitForm();">
   <input type="radio" name="operation" value="1" checked>Answer 1
   <input type="radio" name="operation" value="2">Answer 2
   <p>
   <input type="submit" name="submit" value="save">
   </p>
</form>

</html>

(我不知道我是否应该包括我尝试的下面的例子)

(I don't know if I should include this following example I tried as well)

BTW这是另一个我试过的例子 - 一个发布,但我不能让这个想法工作。我试图获得第一个单选按钮显示'垃圾'而不是'Answer1'作为最初的硬编码。但是我从发布中借用的代码发生错误,我无法解决。

BTW Here is another of the example I tried - a posting but I cannot get this idea to work . I was trying to get the first radio button to display 'junk' instead of 'Answer1' as originally hard coded. But I have an error from code borrowed from posting, that I cannot resolve.

这是从

    <html>
    <form name="myform" onsubmit="return OnSubmitForm();">
       <input type="radio" id = 'first'  name="operation" value="1" checked <label for="alsoFirst"> Answer 1
       <input type="radio" id = 'second'  name="operation" value="2"<label for="alsoSecond">Answer 2

       <p>
       <input type="submit" name="submit" value="save">
       </p>
    </form>



    <script type="text/javascript">
     document.addEventListener('readystatechange', function() {
      // Seems like a GOOD PRACTICE - keeps me from getting type error I was getting

        // https://stackoverflow.com/questions/14207922/javascript-error-null-is-not-an-object

        if (document.readyState === "complete") {
          init();
        }
      });

     function init() {

        console.log ("expect to change -Answer 1- displayed by first button to word junk");


         // this works
        var label = document.getElementById('first').getElementsByTagName('alsoFirst') [0];
        // this does not work
        label.innerHTML = 'junk';
        }

    //http://www.javascript-coder.com/html-form/html-form-action.phtml
    function OnSubmitForm()
    {
      if(document.myform.operation[0].checked == true)
        {
        alert ( "You have selected the first answer" );  
        }
      else
        if(document.myform.operation[1].checked == true)
            {
            alert ( "You have selected the SECOND answer" );  
            }

        if (document.uniqueName.checked == true){
            alert ( "You have selected the THIRD answer" );  
            }

    }

    /*
    <input type="radio" name="sex" id="male" value="male">
            <label for="male">Male</label>
      </input>

    var input = document.getElementById('male');
    var label = input.getElementsByTagName('label')[0];
    label.innerHTML = 'New Text';

    */
    //https://stackoverflow.com/questions/32292962/javascript-how-to-change-radio-button-label-text
    </script>




    </html>




  • 我以前从我的数组获取值,通过插入表格行显示,连接字符串这是有效的,并进入表格,但没有绑定到提交/保存按钮硬编码到原始的< form> 。我仍然打算在桌子上放置无线电应答按钮,但我正在尝试在这里做一个更为基本的例子。

    • I previously got values from my arrays to display by inserting table rows and concatenating strings. This worked, and went into the table, but did not tie into the submit/save button hardcoded into original <form>. I still plan to have radio answer buttons in a table, but I'm trying to make a more basic example here.
    • 推荐答案

      我已经通过document.getElementByTagName()获取标签进行了一些修改,并对OnSubmitForm()函数进行了一些更改。只需将下面的代码和下面的代码粘贴到代码中。

      I Have made some modifications for getting label through document.getElementByTagName() and also some changes to OnSubmitForm() function. And just pasted your code with those changes below and demo link at the end.

       <html>
      <form name="myform" onsubmit="OnSubmitForm();">
         <input type="radio" id = 'first'  name="operation" value="1"
                checked> <label for="alsoFirst"> Answer 1 </label>
         <input type="radio" id = 'second'  name="operation" value="2">
        <label for="alsoSecond">Answer 2</label>
      
         <p>
         <input type="submit" name="submit" value="save">
         </p>
      </form>
      
      
      
      <script type="text/javascript">
       document.addEventListener('readystatechange', function() {
        // Seems like a GOOD PRACTICE - keeps me from getting type error I was getting
      
          // http://stackoverflow.com/questions/14207922/javascript-error-null-is-not-an-object
      
          if (document.readyState === "complete") {
            init();
          }
        });
      
       function init() {
      
          console.log ("expect to change -Answer 1- displayed by first button to word junk");
      
      
           // this works
          var label = document.getElementsByTagName('label') [0];
          // this does not work
          label.innerHTML = 'junk';
          }
      
      //http://www.javascript-coder.com/html-form/html-form-action.phtml
      function OnSubmitForm()
      {
        if(document.getElementById('first').checked == true)
          {
          alert ( "You have selected the first answer" );  
          }
        else
          if(document.getElementById('second').checked == true)
              {
              alert ( "You have selected the SECOND answer" );  
              }
      
        return false;
      }
      
      /*
      <input type="radio" name="sex" id="male" value="male">
              <label for="male">Male</label>
        </input>
      
      var input = document.getElementById('male');
      var label = input.getElementsByTagName('label')[0];
      label.innerHTML = 'New Text';
      
      */
      //http://stackoverflow.com/questions/32292962/javascript-how-to-change-radio-button-label-text
      </script>
      
      
      
      
      </html>
      

      演示: https://jsbin.com/sojojiy/27/edit?html,console,output

      希望这可以帮助。谢谢!

      Hope this helps. Thanks !

      这篇关于javascript - 如何动态更改单选按钮显示的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆