4

我目前正在创建一个使用步骤的捐赠表格。为了一步一步地移动,我正在使用 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。抓住的是,我不知道如何遍历复选框数组(我假设使用 [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
  • 推特引导

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

4

3 回答 3

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
    }

});

为了跳过这一步,我需要看更长的时间,但这应该让你开始......

于 2013-01-09T14:25:02.620 回答
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");
于 2013-01-09T14:30:15.170 回答
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 示例

于 2013-01-09T14:33:14.670 回答