简体   繁体   English

JavaScript函数弄乱了我的单选按钮显示

[英]JavaScript functions messing up my Radio Button displays

I created a form which contains two Radio Buttons. 我创建了一个包含两个单选按钮的表单。

Each radio button displays a separate form, when Clicked. 单击时,每个单选按钮都显示一个单独的表单。

When a button is un-clicked, the form disappears from view (it is not shown) 取消单击按钮后,该窗体从视图中消失(未显示)

I also added a second JavaScript Function, which disables the " SUBMIT " button, if none of the radio buttons are clicked. 我还添加了第二个JavaScript函数,如果没有单击任何单选按钮,它将禁用“ 提交 ”按钮。

The program works fine..........except I have the following problems : 该程序工作正常..........除了我有以下问题:

(a) For some reason, I am able to click BOTH radio buttons! (a)由于某种原因,我可以单击两个单选按钮! Radio-buttons should not allow for more than one selection. 单选按钮不应允许多个选择。 But, in my form, both radio buttons are clickable 但是,在我的表单中, 两个单选按钮都是可单击的

(b) the second JS function is not working; (b)第二个JS功能不起作用; disabling the SUBMIT button is easy. 禁用“提交”按钮很容易。 But...........when I click either of the radio buttons, the SUBMIT button does not enable. 但是...........当我单击任一单选按钮时, “提交”按钮不启用。

Here is the first JS function (for "hiding" the forms under each radio buttons) 这是第一个JS函数(用于“隐藏”每个单选按钮下的表单)

      <script        
     src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
     </script>

        <script type='text/javascript'>
        function displayForm(c) {
            if (c.value == "2") {    
                jQuery('#firstformContainer').toggle('show');
                jQuery('#secondformContainer').hide();
            }
                if (c.value == "1") {
                 jQuery('#secondformContainer').toggle('show');
                 jQuery('#firstformContainer').hide();
            }
        };
        </script> 

And the second function, for disabling the SUBMIT button : 第二个功能是禁用“提交”按钮:

 <script type="text/javascript">
        function fCheck() { 
         document.my_form.submit.disabled 
         =!(document.my_form.formselector1.checked ||       
 document.my_form.formselector2.checked); 
         } 
 </script> 

And here is the form : 这是表格:

    <form name="my_form" id="my_form">
            <input value="1" type="radio" name="formselector" 
    id="formselector1" onClick="displayForm(this); fCheck()"></input>Input 
     Firstname
            <div style="display:none" id="firstformContainer">
                <form id="firstform"> 
                            <input type="text" "16" id="firstname" 
      name="firstname" value="$firstname">
                        </form>
            </div>
            <br>
            <br>
            <input value="2" type="radio" name="formselector" 
       id="formselector2" onClick="displayForm(this); fCheck()">
       </input>Input Surname
            <div style="display:none" id="secondformContainer">
                <form id="secondform">
                    <input type="text" id="surname" name="surname" 
        value="$surname">
                </dd>
                </form>
            </div>
          <br>
          <input type="submit" name="submit" value="REGISTER" disabled>
       </form>

What am I missing? 我想念什么?

UPDATE UPDATE

I have solved the RADIO BUTTON problem. 我已经解决了无线电按钮的问题。

Now, I need to fix the second problem, with the JS function : fCheck () 现在,我需要使用JS函数来解决第二个问题: fCheck()

It is not working. 它不起作用。

I have feeling it's because : I am not calling BOTH functions correctly in the ONCLICK . 我感觉是因为:我没有在ONCLICK中正确调用BOTH函数。

   <input value="1" type="radio" name="formselector" id="radio1"   
   onClick="displayForm(this); javascript:fCheck()"></input>

   <input value="2" type="radio" name="formselector" id="radio2" 
   onClick="displayForm(this); javascript:fCheck()"></input>

You cannot nest forms. 您不能嵌套表格。 This makes the second radio button not inside the form and hence it allows you to select both at the same time. 这使第二个单选按钮不在表单内,因此允许您同时选择两者。 Rewrite your html like this 像这样重写你的html

<form name="my_form" id="my_form">
    <input value="1" type="radio" name="formselector" id="formselector1" onClick="displayForm(this); fCheck()"></input>
    <input value="2" type="radio" name="formselector" id="formselector2" onClick="displayForm(this); fCheck()"></input>
    <input type="submit" name="submit" value="REGISTER" disabled>
</form>
Input Firstname
<div style="display:none" id="firstformContainer">
    <form id="firstform"> 
        <input type="text" "16" id="firstname" name="firstname" value="$firstname">
    </form>
</div>
<br>
<br>
Input Surname            
<div style="display:none" id="secondformContainer">
    <form id="secondform">
        <input type="text" id="surname" name="surname" value="$surname">
    </form>
</div>
<br>

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

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