jQuery检查道具。无线电和取消选中onClick

时间:2013-01-27 07:02:28

标签: javascript jquery html list

所以这是代码

<html>
    <head>
        <link href="../../fuctions/style.css" rel="stylesheet" media="screen" type="text/css" />
        <script type="text/javascript" charset="utf-8" src="../../fuctions/cordova-2.3.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="../../fuctions/functions.js"></script>
        <script type="text/javascript" charset="utf-8" src="../../fuctions/jq.js"></script>
    </head>
    <body>
        <div id="topbar">
            <div id="title"><!--Title-->
                After Landing
            </div>  
            <div id="leftnav">
                <a href="../../index_aerosoft.html">Home</a>
                <a href="katana_checklist_all.html">Overview</a><!--AllChecklist-->
            </div>
            <div id="rightnav">
                <a href="katan_checklist_engineshut-downl.html"id="a_next">Next</a></div><!--NextPage-->
            </div>
            <div id="content">
                <ul class="pageitem">
                    <li class="radiobutton" onClick="checkChanges()">
                        <span class="name">Wing Flaps -<font color="red"> UP
                        </font></span>
                        <input name="1" type="radio" value="other" />
                    </li><!--CheckpunktEnd-->
                    <li class="radiobutton" onClick="checkChanges()">
                        <span class="name">Carburetor Heat -<font color="red"> OFF
                        </font></span>
                        <input name="2" type="radio" value="other" />
                    </li><!--CheckpunktEnd-->
                    <li class="radiobutton" onClick="checkChanges()">
                        <span class="name">Exterior Lights -<font color="red"> as required
                        </font></span>
                        <input name="3" type="radio" value="other" />
                    </li><!--CheckpunktEnd-->
                    <li class="radiobutton" onClick="checkChanges()">
                        <span class="name">Electric Fuel Pump -<font color="red"> OFF
                        </font></span>
                        <input name="4" type="radio" value="other" />
                    </li><!--CheckpunktEnd-->
                </ul>
            </div>
            <div id="footer">
                <a class="noeffect" href="#" onClick="resetChecklist();">Reset Checklist</a><!--ResetChecklist-->
                <br /><br />
                <a class="noeffect" href="#" onClick="openxyzDE();">xyz</a>
            </div>
    </body>
</html>

我有一个功能,检查“收音机已检查并更改颜色”

function checkChanges()
{

    $('.radiobutton font').attr('color', 'red');

    $('.radiobutton :checked').closest('.radiobutton').find('font').attr('color', 'green');
}

$(function()
  {
  checkChanges();
  $('.radiobutton :radio').on('click', checkChanges);
  });

我想在该代码中添加一个函数,取消选中Radio,如果已选中并再次单击(onClick),这也应该将颜色更改为红色。

1 个答案:

答案 0 :(得分:1)

几乎没有更正......

  1. 删除:checked之前的空格。变化:

    $('.radiobutton :checked').closest('.radiobutton').find('font').attr('color', 'green');
    

    要:

    $('.radiobutton:checked').closest('.radiobutton').find('font').attr('color', 'green');
    
  2. 您需要在此处使用input[type="radio"]。变化:

    $('.radiobutton :radio').on('click', checkChanges);
    

    要:

    $('input.radiobutton[type="radio"]').on('click', checkChanges);