如何在这种情况下选择和取消选择按钮

时间:2011-12-05 19:06:29

标签: javascript jquery select button drop-down-menu

我为下拉菜单添加了一个jQuery处理程序,以便在第一个下拉菜单中选择某些内容时,它将操作第二个下拉菜单中的选项。现在,用户所做的是在第一个下拉列表(OptionDropId)菜单中选择选项类型,然后在第二个下拉列表(NumberDropId)中,用户选择所需的答案数。在该下拉菜单下方会出现一个按钮列表,用户根据用户在第二个下拉菜单中选择的数量来选择按钮数。

现在让我们说用户点击两个按钮,然后更改选项类型(第一个下拉菜单),然后仍然选择所选按钮,我希望如果选项类型已更改,则取消选择按钮。

这曾经有用,但由于我已经包含了我的change事件处理程序来绑定我的jquery(需要此代码),所以当选项类型更改时,它不会取消选择按钮。

我需要做些什么来解决这个问题:

下面是我的jquery和select和unselect按钮的功能:

$(document).ready(function ()
{
    var OptDrop = new Array();

    OptDrop.abc = ["",1,2];
    OptDrop.abcd = ["",1,2,3];
    OptDrop.abcde = ["",1,2,3,4];
    OptDrop.trueorfalse = [1];
    OptDrop.yesorno = [1];

    $("#optionDropId").change(function ()
    {
        var selectedValue = $(this).val();
        $("#numberDropId").html("");
        $.each(OptDrop[selectedValue], function (x, y)
        {
            $("#numberDropId").append($("<option></option>").attr("value", y).html(y));
        });
    }); 

    $("#optionDropId").change();
});       

var currenttotal=0;
function getButtons()
{
    document.getElementById("answerA").className="answerBtnsOff";
    document.getElementById("answerB").className="answerBtnsOff";
    document.getElementById("answerC").className="answerBtnsOff";
    document.getElementById("answerD").className="answerBtnsOff";
    document.getElementById("answerE").className="answerBtnsOff";       
    document.getElementById("answerTrue").className="answerBtnsOff";
    document.getElementById("answerFalse").className="answerBtnsOff";
    document.getElementById("answerYes").className="answerBtnsOff";
    document.getElementById("answerNo").className="answerBtnsOff";

    currenttotal=0;
}

function btnclick(btn)
{
    if(document.getElementById("numberDropId").value=="")
    {
        alert('You must first select the number of answers you require from the drop down menu');
        return false;
    }
    if (btn.className=="answerBtnsOn")
    {
        btn.className="answerBtnsOff";
        currenttotal--;
        return false;
    }
    if(document.getElementById("numberDropId").value==currenttotal)
    {
        alert('You are not allowed beyond the limit of the number of answers you require, deselect other button');
        return false;
    }
    if (btn.className=="answerBtnsOff")
    {
        btn.className="answerBtnsOn";
        currenttotal++;
        return false;
    }

}

Html代码如下:

<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);">
  <table id="optionAndAnswer">
    <tr>
      <th colspan="2">
        Option and Answer
      </th>
    </tr>
    <tr>
      <td>Option Type:</td> 
      <td>
        <select name="optionDrop" id="optionDropId" onChange="getDropDown()">
          <option value="">Please Select</option>
          <option value="abc">ABC</option>
          <option value="abcd">ABCD</option>
          <option value="abcde">ABCDE</option>
          <option value="trueorfalse">True or False</option>
          <option value="yesorno">Yes or No</option>
        </select>
      </td>
   </tr>
   <tr>
     <td>Number of Answers:</td>
     <td>
       <select name="numberDrop" id="numberDropId" onChange="getButtons()">
       </select>
     </td>
   </tr>
   <tr>
     <td>Answer</td>
     <td>
       <input class="answerBtns" name="answerAName" id="answerA" type="button" value="A" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerBName" id="answerB" type="button"   value="B" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerCName" id="answerC" type="button"   value="C" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerDName" id="answerD" type="button"      value="D" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerEName" id="answerE" type="button"   value="E" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerTrueName" id="answerTrue" type="button"    value="True" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerFalseName" id="answerFalse" type="button"   value="False" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerYesName" id="answerYes" type="button"   value="Yes" onclick="javascript:btnclick(this);"/>
       <input class="answerBtns" name="answerNoName" id="answerNo"      type="button" value="No" onclick="javascript:btnclick(this);"/>
     </td>
    </tr>
  </table>
</form>

1 个答案:

答案 0 :(得分:0)

您可以使用Jquery的.removeAtrr()函数,如下所述:Best way to unselect a <select> in jQuery?

然后您的更改功能将如下所示:

$("#optionDropId").change(function ()
    {
        var selectedValue = $(this).val();
        $("#numberDropId").html("");
        $.each(OptDrop[selectedValue], function (x, y)
        {
            $("#numberDropId").append($("<option></option>").attr("value", y).html(y));
        });
        $("#numberDropId").removeAttr("selected");
    }); 
相关问题