查找并选中没有ID的复选框并提交表单

时间:2017-02-03 08:04:28

标签: javascript jquery html node.js checkbox

我尝试检查几个复选框,将数据添加到表单并提交。我已经能够填写文本字段并选择小部件,但复选框似乎是另一个问题。以下是其中一个复选框的HTML代码:

<fieldset class="s-lc-rm-cap-l-1"><legend><h2>IVC Library - Room 100E <small>Capacity: 1</small></h2></legend><div>Location: First floor

Room amenities: 
- one table, four chairs
- a white board*
- a tower fan

*Markers are available for checkout at the Circulation Desk</div><div class="checkbox">
                      <label for="sch489796847">
                        <input type="checkbox" name="sid[]" id="sch489796847" value="489796847" data-seq="2281101" class="durc" onchange="checkDur('489796847');">
                        8:00am - 8:30am
                      </label>
                    </div><input type="hidden" name="dur489796847" id="sch489796847dur" value="30"><input type="hidden" name="t_sch489796847" id="t_sch489796847" value="IVC Library - Room 100E 8:00am - 8:30am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796848">
                        <input type="checkbox" name="sid[]" id="sch489796848" value="489796848" data-seq="2281102" class="durc" onchange="checkDur('489796848');">
                        8:30am - 9:00am
                      </label>
                    </div><input type="hidden" name="dur489796848" id="sch489796848dur" value="30"><input type="hidden" name="t_sch489796848" id="t_sch489796848" value="IVC Library - Room 100E 8:30am - 9:00am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796849">
                        <input type="checkbox" name="sid[]" id="sch489796849" value="489796849" data-seq="2281103" class="durc" onchange="checkDur('489796849');">
                        9:00am - 9:30am
                      </label>
                    </div><input type="hidden" name="dur489796849" id="sch489796849dur" value="30"><input type="hidden" name="t_sch489796849" id="t_sch489796849" value="IVC Library - Room 100E 9:00am - 9:30am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796850">
                        <input type="checkbox" name="sid[]" id="sch489796850" value="489796850" data-seq="2281104" class="durc" onchange="checkDur('489796850');">
                        9:30am - 10:00am
                      </label>
                    </div><input type="hidden" name="dur489796850" id="sch489796850dur" value="30"><input type="hidden" name="t_sch489796850" id="t_sch489796850" value="IVC Library - Room 100E 9:30am - 10:00am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796851">
                        <input type="checkbox" name="sid[]" id="sch489796851" value="489796851" data-seq="2281105" class="durc" onchange="checkDur('489796851');">
                        10:00am - 10:30am
                      </label>
                    </div><input type="hidden" name="dur489796851" id="sch489796851dur" value="30"><input type="hidden" name="t_sch489796851" id="t_sch489796851" value="IVC Library - Room 100E 10:00am - 10:30am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796852">
                        <input type="checkbox" name="sid[]" id="sch489796852" value="489796852" data-seq="2281106" class="durc" onchange="checkDur('489796852');">
                        10:30am - 11:00am
                      </label>
                    </div><input type="hidden" name="dur489796852" id="sch489796852dur" value="30"><input type="hidden" name="t_sch489796852" id="t_sch489796852" value="IVC Library - Room 100E 10:30am - 11:00am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796853">
                        <input type="checkbox" name="sid[]" id="sch489796853" value="489796853" data-seq="2281107" class="durc" onchange="checkDur('489796853');">
                        11:00am - 11:30am
                      </label>
                    </div><input type="hidden" name="dur489796853" id="sch489796853dur" value="30"><input type="hidden" name="t_sch489796853" id="t_sch489796853" value="IVC Library - Room 100E 11:00am - 11:30am Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796854">
                        <input type="checkbox" name="sid[]" id="sch489796854" value="489796854" data-seq="2281108" class="durc" onchange="checkDur('489796854');">
                        11:30am - 12:00pm
                      </label>
                    </div><input type="hidden" name="dur489796854" id="sch489796854dur" value="30"><input type="hidden" name="t_sch489796854" id="t_sch489796854" value="IVC Library - Room 100E 11:30am - 12:00pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796855">
                        <input type="checkbox" name="sid[]" id="sch489796855" value="489796855" data-seq="2281109" class="durc" onchange="checkDur('489796855');">
                        12:00pm - 12:30pm
                      </label>
                    </div><input type="hidden" name="dur489796855" id="sch489796855dur" value="30"><input type="hidden" name="t_sch489796855" id="t_sch489796855" value="IVC Library - Room 100E 12:00pm - 12:30pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796856">
                        <input type="checkbox" name="sid[]" id="sch489796856" value="489796856" data-seq="2281110" class="durc" onchange="checkDur('489796856');">
                        12:30pm - 1:00pm
                      </label>
                    </div><input type="hidden" name="dur489796856" id="sch489796856dur" value="30"><input type="hidden" name="t_sch489796856" id="t_sch489796856" value="IVC Library - Room 100E 12:30pm - 1:00pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796857">
                        <input type="checkbox" name="sid[]" id="sch489796857" value="489796857" data-seq="2281111" class="durc" onchange="checkDur('489796857');">
                        1:00pm - 1:30pm
                      </label>
                    </div><input type="hidden" name="dur489796857" id="sch489796857dur" value="30"><input type="hidden" name="t_sch489796857" id="t_sch489796857" value="IVC Library - Room 100E 1:00pm - 1:30pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796858">
                        <input type="checkbox" name="sid[]" id="sch489796858" value="489796858" data-seq="2281112" class="durc" onchange="checkDur('489796858');">
                        1:30pm - 2:00pm
                      </label>
                    </div><input type="hidden" name="dur489796858" id="sch489796858dur" value="30"><input type="hidden" name="t_sch489796858" id="t_sch489796858" value="IVC Library - Room 100E 1:30pm - 2:00pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796859">
                        <input type="checkbox" name="sid[]" id="sch489796859" value="489796859" data-seq="2281113" class="durc" onchange="checkDur('489796859');">
                        2:00pm - 2:30pm
                      </label>
                    </div><input type="hidden" name="dur489796859" id="sch489796859dur" value="30"><input type="hidden" name="t_sch489796859" id="t_sch489796859" value="IVC Library - Room 100E 2:00pm - 2:30pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796860">
                        <input type="checkbox" name="sid[]" id="sch489796860" value="489796860" data-seq="2281114" class="durc" onchange="checkDur('489796860');">
                        2:30pm - 3:00pm
                      </label>
                    </div><input type="hidden" name="dur489796860" id="sch489796860dur" value="30"><input type="hidden" name="t_sch489796860" id="t_sch489796860" value="IVC Library - Room 100E 2:30pm - 3:00pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796861">
                        <input type="checkbox" name="sid[]" id="sch489796861" value="489796861" data-seq="2281115" class="durc" onchange="checkDur('489796861');">
                        3:00pm - 3:30pm
                      </label>
                    </div><input type="hidden" name="dur489796861" id="sch489796861dur" value="30"><input type="hidden" name="t_sch489796861" id="t_sch489796861" value="IVC Library - Room 100E 3:00pm - 3:30pm Friday, February 10, 2017"><div class="checkbox">
                      <label for="sch489796862">
                        <input type="checkbox" name="sid[]" id="sch489796862" value="489796862" data-seq="2281116" class="durc" onchange="checkDur('489796862');">
                        3:30pm - 4:00pm
                      </label>
                    </div><input type="hidden" name="dur489796862" id="sch489796862dur" value="30"><input type="hidden" name="t_sch489796862" id="t_sch489796862" value="IVC Library - Room 100E 3:30pm - 4:00pm Friday, February 10, 2017"><a href="#bform" class="sr-only">Skip to registration form</a></fieldset>

我没有直接识别复选框的简单方法,但隐藏的输入值类似于&#34; IVC图书馆 - 房间100E上午8:00 - 上午8:30,2017年2月10日星期五&#34;我可以创造。我想使用该值来检查框(但是该值小部件似乎是一个复选框。有没有办法让复选框变为真?

EDIT(当前NodeJS和JQuery代码):

require("jsdom").env("http://ivc.libcal.com/rooms_acc.php?gid=962&d=2017-02-10&cap=0", function (err, window) {
    if (err) {
        console.error(err);
        return;
    }

   var $ = require("jquery")(window);
   $(window).ready(function () {

       var checkbox = $("#roombookingform :input[value='IVC Library - Room 100E 8:00am - 8:30am Friday, February 10, 2017']");
        if (checkbox.length) {
            console.log("Checkbox exists.");
            checkbox.prop('checked', true);
            $('#roombookingform #fname').val("FNAME");
            console.log($('#roombookingform #fname').val());
            $('#roombookingform #lname').val("LNAME");
            console.log($('#roombookingform #lname').val());
            $('#roombookingform #email').val("myemail");
            console.log($('#roombookingform #email').val());
            $('#roombookingform #q1').val("3");
            console.log($('#roombookingform #q1').val());
            $('#roombookingform #q2').val("29292929");
            console.log($('#roombookingform #q2').val());

            console.log("\n\n-------------------\n\n");

            console.log($('#roombookingform #sch489796848').val());
            console.log($('#roombookingform #fname').val());
            console.log($('#roombookingform #lname').val());
            console.log($('#roombookingform #email').val());
            console.log($('#roombookingform #q1').val());
            console.log($('#roombookingform #q2').val());

            $("#roombookingform #s-lc-rm-ac-but").click(function () {
                // do something when button is clicked
                console.log("Button clicked.");
            });          
            $("#roombookingform :button[id='s-lc-rm-ac-but']").click();    
        }

    });
});

它也没有提交......我因为复选框问题而猜测它。

我正在寻找一种方法来使用除id之外的某种方式来识别该复选框,因为没有可靠的方法来跟踪id。

0 个答案:

没有答案