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);
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数