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

发布于 2024-12-19 18:27:18 字数 4942 浏览 0 评论 0原文

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

现在假设用户单击两个按钮,然后更改选项类型(第一个下拉菜单),然后所选按钮仍处于选中状态,我希望如果选项类型已更改,则取消选择按钮。

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

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

下面是我的jquery以及选择和取消选择按钮的功能:

$(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>

I included a jQuery handler for my dropdown menus so that when something is selected in the first dropdown menu, it will manipulate the options in the second dropdown menu. Now what the user does is pick the option type in the first dropdown (OptionDropId) menu and then in the second dropdown (NumberDropId) menu the user selects the number of answers wanted. Below that dropdown menu a list of buttons would appear and the user selects the number of buttons depending on how many the user has selected in the second dropdown menu.

Now let's say user clicks on two buttons and then changes the option type (first dropdown menu), then the buttons selected are still selected, I want the buttons to be unselected if the option type has changed.

This used to work but since I have included my change event handler to bind my jquery (this code is needed), it does not unselect buttons when the option type changes.

What do I need to do to fix this:

Below is my jquery and function of select and unselect buttons:

$(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 code is below:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-12-26 18:27:18

您可以使用 Jquery 的 .removeAtrr() 函数,如下所述:最佳方法取消选择

那么你的更改函数将如下所示:

$("#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");
    }); 

You can just use Jquery's .removeAtrr() function as described here: Best way to unselect a <select> in jQuery?

Then your change function would look like this:

$("#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");
    }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文