dongxiegao3071 2018-12-14 15:46
浏览 220

如何在提交表单时获取特定按钮ID?

<form action="" method="post" id="all-student-reservations-form" class="form-block">
    <label style="display: inline-block; vertical-align: bottom;">Type<br>
                <select name="drlist-type" id="drlist-type" class="input-large">
                    <?php foreach (array('All', 'Midterm', 'Final') as $examType) { ?>
                        <option <?php if ($type == $examType) echo 'selected' ?>><?php echo $examType ?></option>
                    <?php } ?>
                </select>
            </label>
            <label style="display: inline-block; vertical-align: bottom;">Campus<br>
                <?php echo campus_selector('drlist-campus', $campuses, $selectedCampus); ?>
            </label>
            <button type="submit" class="btn-large" id="report" name="report">View All Students Reservations</button>
            <button type="submit" class="btn-large" id="download" name="report">Download Report</button>
            <input type="hidden" name="postId" value="" />
</form>

$("#all-student-reservations-form").on("submit", function (e) {
            e.preventDefault();

            $('.btn-large').click(function()) {
                var postId = $('#postId').val($(this).attr('id'));
            });

            alert(postId);
            var type = $("#drlist-type").val(),
                campus = $("#drlist-campus select").val();
            window.location = site_url("reservations/studentsbyCurrentTerm/"+campus+'/'+type);
        });

Didn't get PostId value when clicking on View/Download.

  • 写回答

1条回答 默认 最新

  • dongqie4402 2018-12-14 15:54
    关注

    First you need to change the button type to button and trigger a function which set the postId then just submit your form.

    <form action="" method="post" id="all-student-reservations-form" class="form-block">
    <label style="display: inline-block; vertical-align: bottom;">Type<br>
                <select name="drlist-type" id="drlist-type" class="input-large">
                    <?php foreach (array('All', 'Midterm', 'Final') as $examType) { ?>
                        <option <?php if ($type == $examType) echo 'selected' ?>><?php echo $examType ?></option>
                    <?php } ?>
                </select>
            </label>
            <label style="display: inline-block; vertical-align: bottom;">Campus<br>
                <?php echo campus_selector('drlist-campus', $campuses, $selectedCampus); ?>
            </label>
            <button type="button" class="btn-large" onclick="submitForm('report')" name="report">View All Students Reservations</button>
            <button type="button" class="btn-large" onclick="submitForm('download')" name="report">Download Report</button>
            <input type="hidden" name="postId" value="" />
    

    function submitForm(id){
        $("#postId").val(id);
        $("form").submit();
    }
    $("#all-student-reservations-form").on("submit", function (e) {
            e.preventDefault();
    
            var postId = $('#postId').val();
            alert(postId);
            var type = $("#drlist-type").val(),
                campus = $("#drlist-campus select").val();
            window.location = site_url("reservations/studentsbyCurrentTerm/"+campus+'/'+type);
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!