简体   繁体   English

如何将输入数据保存到我所有的 forms

[英]How to persist input data to all of my forms

I'm building an input form project for my job and I'm running into a problem with persisting a certain input value.我正在为我的工作构建一个输入表单项目,但我遇到了保持某个输入值的问题。 When the user creates a new project, they have to give that project a project ID.当用户创建一个新项目时,他们必须为该项目提供一个项目 ID。 I have 5 forms that the user goes through and I want them to be able to enter the project ID in once on the first form, and then have it persist to the other 4 forms without them having to type it in again.我有 5 个 forms 供用户使用,我希望他们能够在第一个表单上一次输入项目 ID,然后让它持续到其他 4 个 forms 而无需再次输入。

This is the html form where I want to pull the project ID and persist it to the rest of my forms.这是 html 表单,我想在其中提取项目 ID 并将其保存到我的 forms 的 rest 中。 I want to pull the value using the id "pid" and then when the button at the bottom is clicked with id of 'data' I want the value to be pushed into a variable.我想使用 id“pid”提取值,然后当单击底部的按钮并使用 id 为“data”时,我希望将值推送到变量中。

  <form action="/metadata" method="POST">
                <div class="row">
                    <div class="col-md-4">
                        <div>
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" id="inputGroup-sizing-default">Project ID:</span>
                                </div>
                                <input name="project_id" type="text" id="pid" class="form-control">
                            </div>
                        </div>
                        <div class="input-group mb-3">
                            <label class="input-group-text" for="inputGroupSelect01">Building:</label>
                            <select name="building" class="form-select" id="inputGroupSelect01">
                                <option disabled selected>Choose...</option>
                                {{#each bldgs}}
                                <option>{{acronym}}</option>
                                {{/each}}
                            </select>
                        </div>
                        <div class="input-group mb-3">
                            <label class="input-group-text" for="inputGroupSelect01">Measure Type:</label>
                            <select name="measure_type" class="form-select" id="inputGroupSelect01">
                                <option disabled selected>Choose...</option>
                                <option>AHU DDC</option>
                                <option>AHU DDC &amp; Scheduling</option>
                                <option>Data Center</option>
                                <option>EBCx</option>
                                <option>EMOCx</option>
                                <option>Exhaust System Optimization</option>
                                <option>Holiday Scheduling</option>
                                <option>Lighting</option>
                                <option>Optimum Energy</option>
                                <option>Scheduling</option>
                                <option>Valves</option>
                                <option>VFD Supply Fan</option>
                                <option>Water</option>
                                <option>Other</option>
                            </select>

                        </div>
                        <div class="input-group mb-3">
                            <label class="input-group-text" for="inputGroupSelect01">Status:</label>
                            <select name="status" class="form-select" id="inputGroupSelect01">
                                <option disabled selected>Choose...</option>
                                <option>0-Cancelled</option>
                                <option>1-Planning</option>
                                <option>2-In Progress</option>
                                <option>3-In Reporting Period</option>
                                <option>5-Completed</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-4 mt-5">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-default">Start Date:</span>
                            </div>
                            <input placeholder="mm/dd/yy" name="baseline_start_date" type="text" class="form-control" />
                            <input placeholder="mm/dd/yy" name="reporting_period_start_date" type="text"
                                class="form-control" />
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-default"># of Days:</span>
                            </div>
                            <input placeholder="mm/dd/yy" name="length_baseline_period_days" type="text"
                                class="form-control" />
                            <input placeholder="mm/dd/yy" name="length_reporting_period_days" type="text"
                                class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-4 mt-5">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-default">Staff Lead:</span>
                            </div>
                            <select name="staff_lead" class="form-select" id="inputGroupSelect01">
                                <option disbaled selected>Choose...</option>
                                <option>Adam Keeling</option>
                                <option>Amanda Berens</option>
                                <option>Buddy Bishop</option>
                                <option>Dave Cooper</option>
                                <option>Grace Hsieh</option>
                                <option>John Milton</option>
                                <option>Matt Stevens</option>
                                <option>Meagan Jones</option>
                                <option>Pat Mazur</option>
                                <option>Richard Shearman</option>
                                <option>Travis Isakson</option>
                            </select>
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-default">Staff Support:</span>
                            </div>
                            <select name="staff_colead" class="form-select" id="inputGroupSelect01">
                                <option disbaled selected>Choose...</option>
                                <option>Adam Keeling</option>
                                <option>Amanda Berens</option>
                                <option>Buddy Bishop</option>
                                <option>Dave Cooper</option>
                                <option>Grace Hsieh</option>
                                <option>John Milton</option>
                                <option>Matt Stevens</option>
                                <option>Meagan Jones</option>
                                <option>Pat Mazur</option>
                                <option>Richard Shearman</option>
                                <option>Travis Isakson</option>
                            </select>
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text" id="inputGroup-sizing-default">Analyst:</span>
                            </div>
                            <select name="analyst" class="form-select" id="inputGroupSelect01">
                                <option disabled selected>Choose...</option>
                                <option>Jeff McComas</option>
                                <option>Josh Butler</option>
                                <option>Laura Chiasson</option>
                                <option>Terrell McRee</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="input-group mt-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text">Project Description:</span>
                    </div>
                    <textarea rows='5' name="project_description" class="form-control"
                        aria-label="With textarea"></textarea>
                </div>
                <div class="input-group mt-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text">Nonenergy Benefits:</span>
                    </div>
                    <textarea rows='2' name="nonenergy_benefits" class="form-control"
                        aria-label="With textarea"></textarea>
                </div>
                <div class="text-end">
                    <button type="submit" id="data" style="background-color: #bf5700;"
                        class="btn text-light btn-warning  mt-3">Next</button>
                </div>
            </form>

This is my js file.这是我的 js 文件。 Each on click is a different page.每次点击都是不同的页面。 However each form has the same input of project ID with a class of "project_id".但是,每个表单都具有相同的项目 ID 输入,其中 class 为“project_id”。 So I try to $(".project_id").val(projectId) but when I move on the next form nothing happens.所以我尝试 $(".project_id").val(projectId) 但是当我继续下一个表单时没有任何反应。 Each form has an input value with a class of "project_id" and thats where I try to set it based on the input from the first form.每个表单都有一个输入值,其 class 为“project_id”,这就是我尝试根据第一个表单的输入设置它的地方。

This is a link to the repo if it helps to see all the code.如果它有助于查看所有代码,这是一个指向 repo 的链接。 https://github.com/oballematt/ecm_input_forms https://github.com/oballematt/ecm_input_forms

Any advice on how to achieve this is greatly appreciated!非常感谢任何有关如何实现这一目标的建议! Thanks!谢谢!

 const projectId = []
   
    $("#data").on('submit', () => {
        projectId.push($('#pid').val())
    })

  $('.project_id').val(projectId)

    $('#tableData').on('click', 'button.addRow', function(e) {
        const cloneRow = $('#tableData tbody tr').first();
        e.preventDefault();
        let data = {
         project_id: $(".project_id").last().val(),
         imp_or_ann: $(".imp_or_ann").last().val(),
         category: $(".category").last().val(),
         cost: $(".cost").last().val(),
         hours: $(".hours").last().val()
    }
        $.ajax({
            url: '/costs_hours',
            type: 'POST',
            data: data
        }).then(
            cloneRow.clone().appendTo('#tableData tbody').find(".cost, .hours").val(''),
            $("#next").removeAttr('disabled'),
            $("#link").attr('href', '/fundings'),
        )
    })

    $('#tableData').on('click', 'button.addRowF', function(e) {
        const cloneRow = $('#tableData tbody tr').first();
        e.preventDefault(); 
        let data = {
         project_id: $(".project_id").last().val(),
         source: $(".source").last().val(),
         implementation: $(".implementation").last().val(),
         annual: $(".annual").last().val(),
    }
        $.ajax({
            url: '/fundings',
            type: 'POST',
            data: data
        }).then(
            cloneRow.clone().appendTo('#tableData tbody').find(".implementation, .annual").val(''),
            $("#next").removeAttr('disabled'),
            $("#link").attr('href', '/baseline')
        )
    })

    $('#tableData').on('click', 'button.addRowB', function(e) {
        const cloneRow = $('#tableData tbody tr').first();
        e.preventDefault();
        let data = {
         project_id: $(".project_id").last().val(),
         commodity: $(".commodity").last().val(),
         unit: $(".unit").last().val(),
         value: $(".value").last().val(),
    }
        $.ajax({
            url: '/baseline',
            type: 'POST',
            data: data
        }).then(
            cloneRow.clone().appendTo('#tableData tbody').find(".value").val(''),
            $("#next").removeAttr('disabled'),
            $("#link").attr('href', '/savings')
        )

    })

    $('#tableData').on('click', 'button.addRowS', function(e) {
        const cloneRow = $('#tableData tbody tr').first();
        e.preventDefault();
        let data = {
         project_id: $(".project_id").last().val(),
         phase: $(".phase").last().val(),
         commodity: $(".commodity").last().val(),
         unit: $(".unit").last().val(),
         value: $(".value").last().val()
    }
        $.ajax({
            url: '/savings',
            type: 'POST',
            data: data
        }).then(
            cloneRow.clone().appendTo('#tableData tbody').find(".value").val(''),
            $("#next").removeAttr('disabled'),
            $("#link").attr('href', '/')
        )

    })

To persist values across pages, several solutions can be used: localStorage, queryString, State machines.要跨页面保留值,可以使用多种解决方案:localStorage、queryString、State 机器。

Here the problem comes from that val(projectId) is not defined since you reload a new page.这里的问题来自val(projectId)没有定义,因为您重新加载了一个新页面。

Depending on your constraint for your app, I would advice you to use queryString since it is very straightforward (just add the query in the URL and you can retrieve it with JS).根据您对应用程序的约束,我建议您使用 queryString,因为它非常简单(只需在 URL 中添加查询,您可以使用 JS 检索它)。

https://en.wikipedia.org/wiki/Query_string https://en.wikipedia.org/wiki/Query_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM