简体   繁体   English

使用jQuery更新DOM元素

[英]Updating DOM elements using jQuery

I'm looking for a way to update the webpage I'm working on to act as a report for several different people to pass back and forth. 我正在寻找一种方法来更新正在处理的网页,以作为报告供多个不同的人来回传递。 I'm using forms to take in several pieces of data and am wondering how I can make it so that it just immediately adds the content to the divs under the right heading. 我正在使用表单来接收多个数据,并且想知道如何才能使它立即将内容添加到正确标题下的div中。 I'm currently using jquery and append and it looks like it adds the desired input and then immediately removes it. 我当前正在使用jquery并追加,它看起来像添加所需的输入,然后立即将其删除。 I tried using .live as well and it did not show up at all. 我也尝试使用.live,但它根本没有出现。 Is there a way to make form inputs post to the page without submitting to another page? 有没有一种方法可以使表单输入发布到页面而不提交到另一个页面?

Here is my code so far, testing just the element that will be the heading for the issue: 到目前为止,这是我的代码,仅测试将成为问题标题的元素:

<div class="IssueDiv">

</div>

<form id="newIssue">
    <fieldset>
        <legend>Add a new important issue:</legend>
        <input type="text" id="issue" placeholder="Issue Summary...">
        <input type="text" id="issue-client" placeholder="Client...">
        <input class="ticket" type="text" id="issueParent" placeholder="Parent ticket..."><br>
        <textarea placeholder="Issue details..."></textarea><br>
        <button id="addIssue">Add Issue</button>
    </fieldset>
</form>

And the jquery: 和jQuery:

<script>
    $(function(){
        $("#addIssue").click(function() {
            var $issue = $("#issue").val();
            var $issueSum = $("<h3></h3>").text($issue);
            $(".IssueDiv").append($issueSum);
        });
    });
</script>

edit: I'm looking into using AJAX but I'm not sure how to make it so that all of the input data will persist. 编辑:我正在考虑使用AJAX,但我不确定如何使它,以便所有输入数据将保持不变。 I am basically looking to make a webpage-style-report that will allow myself and my team to update the entries on the report and they will stay on the report until we are able to take them off by removing a div that encapsulates the individual issue. 我基本上是想制作一个网页样式的报告,该报告将允许我本人和我的团队更新报告中的条目,并且它们将保留在报告中,直到我们能够通过移除封装了各个问题的div取消它们为止。

I would also like to be able to format the individual pieces here separately, so, for instance, I could add a check-box that says the issue is urgent and format the heading of those to be red. 我还希望能够在此处分别格式化各个片段,因此,例如,我可以添加一个复选框,指出该问题很紧急,并将其标题格式化为红色。 What is the easiest way to have data that persists, can be added into new (div/h/p) elements, and is shown on the main webpage, while also allowing me to update formatting? 使数据持久保存,可以添加到新的(div / h / p)元素并显示在主网页上,同时还允许我更新格式的最简单方法是什么?

Your code appears to add the text and then immediately remove it because your form gets posted and the page reloads, effectively resetting the page to its initial state. 您的代码似乎添加了文本,然后立即将其删除,因为您的表单已发布并且页面重新加载,从而有效地将页面重置为其初始状态。

If you just want to add the text to the page without posting the form or executing any server-side processing, you can prevent the form from posting using jQuery's preventDefault() . 如果只想将文本添加到页面而不发布表单或执行任何服务器端处理,则可以使用jQuery的preventDefault()阻止表单发布。 Note that I have created a submit listener on the form itself, rather than a click listener on the submit button. 请注意,我已经在表单本身上创建了一个submit侦听器,而不是在“提交”按钮上click了一个侦听器。

$("#newIssue").on('submit', function(e) {
    e.preventDefault();
    ...
});

 $(function () { $("#newIssue").on('submit',function (e) { e.preventDefault(); var $issue = $("#issue").val(); var $issueSum = $("<h3></h3>").text($issue); $(".IssueDiv").append($issueSum); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="IssueDiv"></div> <form id="newIssue"> <fieldset> <legend>Add a new important issue:</legend> <input type="text" id="issue" placeholder="Issue Summary..."> <input type="text" id="issue-client" placeholder="Client..."> <input class="ticket" type="text" id="issueParent" placeholder="Parent ticket..."> <br> <textarea placeholder="Issue details..."></textarea> <br> <button id="addIssue">Add Issue</button> </fieldset> </form> 

However, keep in mind that if you're using this to share reports between computers, this will not work. 但是,请记住,如果使用此功能在计算机之间共享报告,则将无法使用。 This is only updating the DOM in the current browser and is not doing any data storage or retrieval. 这仅在当前浏览器中更新DOM,并且不进行任何数据存储或检索。 If you need the reports to update online, consider using AJAX to post your data to a server-side script without refreshing the page. 如果您需要报告进行在线更新,请考虑使用AJAX将数据发布到服务器端脚本中,而无需刷新页面。 Then include some sort of timer that refreshes the content (also using AJAX) on a schedule (eg every 10 seconds). 然后包括某种计时器,可以按计划(例如,每10秒)刷新内容(也使用AJAX)。

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

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