jQuery + Ajax:向控制器提交单个文本框

发布于 2024-11-08 12:29:55 字数 648 浏览 0 评论 0原文

我正在开发时间表应用程序。我想添加自动保存功能。 假设我有一个这样的表单:

<form id="timesheet" action="save_timesheet.html" method="POST">
<input type="text" id="day1taskid2" />
<input type="text" id="day2taskid2" />
<input type="text" id="day3taskid2" />
<input type="text" id="day41taskid2" />
...
</form>

如果用户插入一个新值 (= onChange),我想获取该特定值并将其发送到我的控制器(使用 JSON/Ajax)。例如:我向特定文本框添加 2 小时,jQuery ajax 应该执行更新 onChange。很难找到一个可行的例子。我见过很多使用表单的示例,但我没有提交整个表单,只提交一个小文本框。一个屏幕上可以有 70 多个文本框,具体取决于要显示的任务量。

我需要什么?您能为我举一个易于理解的例子来说明这个案例吗?我正在使用 Spring 2.x。

添加了解决方案;-)

I'm working on a timesheet application. I'd like to add an autosave functionality.
Let's say I have a form like this:

<form id="timesheet" action="save_timesheet.html" method="POST">
<input type="text" id="day1taskid2" />
<input type="text" id="day2taskid2" />
<input type="text" id="day3taskid2" />
<input type="text" id="day41taskid2" />
...
</form>

If a user inserts a new value (= onChange) I'd like to get that specific value and send it to my controller (using JSON/Ajax). For example: I add 2 hours to a specific textbox, and the jQuery ajax should do the update onChange. It's pretty hard to find a working example. I've seen a lot of examples using a form, but I'm not submitting the whole form, only one small textbox. There can be over 70 textboxes on one screen, depending on the amount of tasks to show.

What do I need? Could you give me an easy-to-understand example for this case? I'm using Spring 2.x.

Added the solution ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-11-15 12:29:55

您可以使用当前所在文本框中的内容进行手动 ajax 发布。然后在您的服务器代码中处理传入的内容:

$("[input='text']").change(function() { 
    var $this = $(this);
    $.ajax({
        url: "url/to/your/server",
        data: { name: $this.attr("id"), value: $this.val() },
        success: function(data) {
            console.log(data);
        }
    });
});

这将向您的服务器发送一个键值对,其中键是文本框的 id ,该值将是实际值。

因此,在您的服务器代码中,您只需检查密钥,并相应地处理该值。

You could do a manual ajax post with the content from the textbox you're currently in. Then in your server code handle what ever comes in:

$("[input='text']").change(function() { 
    var $this = $(this);
    $.ajax({
        url: "url/to/your/server",
        data: { name: $this.attr("id"), value: $this.val() },
        success: function(data) {
            console.log(data);
        }
    });
});

This will send a key-value pair to your server with the key being the id of the textbox, and the value would be the actual value.

So in your server code you could just check the key, and handle the value accordingly.

骄兵必败 2024-11-15 12:29:55

我使用 jQuery AJAX 来调用我的控制器。

我的观点:

function doUpdate(id, taskid){
        var x = id.value;
        var firstday = document.getElementById('firstweek').value;
        var pid = document.getElementById('projectid').value;

        if (x >=0 && x <= 24 && x != ""){
            $.post("savets.html", { hours: x, id: id.id, taskid: taskid, firstday: firstday, pid: pid }, function(data) {
                alert("callback");  
            });  
        } else {
            alert("Please enter a valid number: 0 - 24");
        }

我的控制器:

@RequestMapping(value = { "/savets.html" }, method = RequestMethod.POST)
public String addTimeRecord(ModelMap model, HttpServletRequest request,
        ) {
    System.out.println("Doing the insert onUpdate with value = "
            + request.getParameter("hours") + " for ID "
            + request.getParameter("id") + " " 
            + request.getParameter("firstday")  + " " 
            + request.getParameter("taskid"));

形式:

<input type="text" onChange=doUpdate(id of my record, the taskid) />

其余的都是不言自明的。我的表单中隐藏了值,我使用 Javascript 获取它们。之后,我调用将执行更新的控制器(我没有添加此代码,因为它是一个简单的检查+插入)

I'm using jQuery AJAX for the call to my controller.

My view:

function doUpdate(id, taskid){
        var x = id.value;
        var firstday = document.getElementById('firstweek').value;
        var pid = document.getElementById('projectid').value;

        if (x >=0 && x <= 24 && x != ""){
            $.post("savets.html", { hours: x, id: id.id, taskid: taskid, firstday: firstday, pid: pid }, function(data) {
                alert("callback");  
            });  
        } else {
            alert("Please enter a valid number: 0 - 24");
        }

My Controller:

@RequestMapping(value = { "/savets.html" }, method = RequestMethod.POST)
public String addTimeRecord(ModelMap model, HttpServletRequest request,
        ) {
    System.out.println("Doing the insert onUpdate with value = "
            + request.getParameter("hours") + " for ID "
            + request.getParameter("id") + " " 
            + request.getParameter("firstday")  + " " 
            + request.getParameter("taskid"));

Form:

<input type="text" onChange=doUpdate(id of my record, the taskid) />

The rest is selfexplanatory. I have hidden values in my form, I get them using Javascript. After that I call the controller which will execute the update (I did not add this code, as it is a simple check + insert)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文