如果仅选中一个复选框,如何向前跳一步并设置值

时间:2013-01-09 14:18:43

标签: javascript jquery html

我正在创建一个使用步骤的捐赠表单。要从一步到另一步,我使用jQuery(特别是以下函数):

function showNextStep(currentStep) {
    $("#step" + currentStep).slideToggle("slow", function () {
        if (currentStep === 1) {
            //figure out what kind of donation they are making
            var chosenDonationType = $("[name=donationType]").val();
            //show the apppropriate slide
            switch (chosenDonationType) {
            case "oneTimeGift":
                currentStep += 1;
                $("#makingAOneTimeGift").show();
                $("#step" + currentStep).slideToggle("slow");
                break;
            case "recurringDonation":
                currentStep += 1;
                $("#makingARecurringGift").show();
                $("#step" + currentStep).slideToggle("slow");
                break;
                //if somehow they changed it to something else, ignore them and return false.
            default:
                return false;
                //break; not needed due to return
            }//end switch
        } else {
            currentStep += 1;
            $("#step" + currentStep).slideToggle("slow");
        }
    });
}

我有一个用户可以捐赠的资金清单。此后的步骤(id =“step4”)用于分配。如果用户只选择一个复选框,我想跳过该步骤并将相应的输入值设置为100。 Catch是,我不知道如何运行复选框数组(我假设使用[name = list-item])并发现只选择了一个,确定哪一个,然后跳过下一步骤,将相应分配框的值设置为100.什么是性能有效的方法?

复选框使用以下样式:

<label class="checkbox">
    <input type="checkbox" id="showGoodMenGoodCitizensAllocation" name="list-items[]" value="Good_Men_Good_Citizens" />
    Good Men, Good Citizens Scholarship
</label>
<label class="checkbox">
    <input type="checkbox" id="showClassof2012Allocation" name="list-items[]" value="Class_Of_2012" />
    Class of 2012 Scholarship <abbr title="In Honor Of">IHO</abbr> Mr. Jason M. Ferguson &rsquo;96
</label>
<label class="checkbox">
    <input type="checkbox" id="showClassof2011Allocation" name="list-items[]" value="Class_Of_2011" />
    Class of 2011 Scholarship <abbr title="In Honor Of">IHO</abbr> Ms. Anita Garland
</label>

分配输入如下:

<div id="GoodMenGoodCitizensAllocation" class="input-append hiddenByDefault">
    <input type="number" name="Good_Men_Good_Citizens-Allocation" min="0" max="100" value="0" />
    <span class="add-on">&#37; to the Good Men, Good Citizens Scholarship</span>
</div>
<div id="ClassOf2012Allocation" class="input-append hiddenByDefault">
    <input type="number" name="Class_Of_2012-Allocation" min="0" max="100" value="0" />
    <span class="add-on">&#37; to the Class of 2012 Scholarship <abbr title="In Honor Of">IHO</abbr> Mr. Jason M. Ferguson &rsquo;96</span>
</div>
<div id="ClassOf2011Allocation" class="input-append hiddenByDefault">
    <input type="number" name="Class_Of_2011-Allocation" min="0" max="100" value="0" />
    <span class="add-on">&#37; to the Class of 2011 Scholarship <abbr title="In Honor Of">IHO</abbr> Ms. Anita Garland</span>
</div>

对于整页代码: http://pastebin.com/yFv2day1

对于目前使用的完整javascript代码: http://pastebin.com/P0YVRuqY

我正在使用的资源:

  • PHP
  • jQuery 1.8.3
  • Twitter Bootstrap

提前感谢大家的时间和帮助。

3 个答案:

答案 0 :(得分:1)

迭代一系列复选框并找出检查的内容:

$("#YOUR-FORM input[type=checkbox]").each(function (i, e) {

    if ($(e).attr("checked") == "checked") {
        // This checkbox is checked... do some stuff
    } else {
        // Not checked...ignore
    }

});

对于跳过这一步,我需要长时间看一下,但这应该让你开始......

答案 1 :(得分:1)

试试这个代码段:

var $chck = $("#step3 :checkbox:checked");
if ($chck.length === 1) {
  var selectedVal = $chck.val();//do whatever you want with that
  currentStep += 2;
} else 
  currentStep++;  

$("#step" + currentStep).slideToggle("slow");

答案 2 :(得分:1)

以下是我点击按钮时运行的示例:

$(function () {
  $('#evaluate').click(function () {
    var checked = $('input[name="list-items[]"]:checked');
    if (checked.length == 1) {
      alert('one checked: ' + checked.val());
    } else {
      var vals = [];
      checked.each(function () {
        vals.push($(this).val());
      });
      alert(checked.length + " boxes checked: " + vals);
      }
    })
  });

jsfiddle example