Ajax 将值从文本框保存到服务器

发布于 2024-12-24 16:35:39 字数 1192 浏览 3 评论 0原文

刚刚发现 Ajax 是什么,我想构建一个简单的 Ajax/Jquery 函数,它获取文本框的值并在单击按钮时将其发布到服务器。该按钮是一个带有悬停事件的 ui 图标到它附加在按键上..我尝试将它放在一起,但是 firebug 控制台读取语法错误..请帮助..

    $(function () {
        $('.solo1').after('<span class="ui-state-default ui-corner-all ui-icon-disk ui-icon saveButton" title="Save" style="float:left; height:20px;" onclick="function save (value)"></span><br />')// ui icon
    .keypress(function() {
        $(this).next('.saveButton').show();//appends ui icon
    });
});


function save(value) {
    $.ajax({
        url: "/*ServerURL*/",
        type: "POST",
        data: '{ $("#craven1") : "' + value + '" }',
        data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1").val() + "}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            console.log(data);
        },
    });
}
</script> 

<div id="featherbone">
<input class="solo1" id="craven1"/>
<input class="solo1" id="craven2"/>
<input class="solo1" id="craven3"/>
<input class="solo1" id="craven4"/>
<input class="solo1" id="craven5"/>
</div>

Just found out what Ajax is, and I want to build a simple Ajax/Jquery function that takes the value of a text-box and posts it to the server on a button click.. The button is a ui icon with a hover event attached to it which appends on key-press.. I've tried putting it together, but firebug console reads syntax error.. Please help..

    $(function () {
        $('.solo1').after('<span class="ui-state-default ui-corner-all ui-icon-disk ui-icon saveButton" title="Save" style="float:left; height:20px;" onclick="function save (value)"></span><br />')// ui icon
    .keypress(function() {
        $(this).next('.saveButton').show();//appends ui icon
    });
});


function save(value) {
    $.ajax({
        url: "/*ServerURL*/",
        type: "POST",
        data: '{ $("#craven1") : "' + value + '" }',
        data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1").val() + "}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            console.log(data);
        },
    });
}
</script> 

<div id="featherbone">
<input class="solo1" id="craven1"/>
<input class="solo1" id="craven2"/>
<input class="solo1" id="craven3"/>
<input class="solo1" id="craven4"/>
<input class="solo1" id="craven5"/>
</div>

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

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

发布评论

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

评论(2

风透绣罗衣 2024-12-31 16:35:40

单击 UI 按钮时是否收到错误消息?在这种情况下,我认为语法问题出在 .solo1 元素之后添加的 HTML 中,特别是在 onclick 属性中:

$('.solo1').after('<span ... onclick="function save (value)"</span><br/>')

应该是:

$('.solo1').after('<span ... onclick="save (value)"</span><br/>')

因为您希望调用函数 save (未定义,因此删除了关键字来自 onclick 属性的 function)。

但是查看您的代码并阅读您的描述,我认为您希望将与按钮相邻的输入元素的值传递到函数中?

在这种情况下,更好的解决方案是首先创建 UI 按钮,然后为每个按钮添加单击处理程序:

// Create button and then add it after each input
var button = $('<span ... ></span><br/>');
$('.solo1').after(button);

// Now that the buttons are created, find them and give each a click handler
$('.saveButton').click(function() {
  // Get value from the input element that this button was added after
  var value = $(this).prev().val();

  // Now call the save method with the value
  save(value);
});

工作示例: http: //jsfiddle.net/h78hw/

Do you get the error message when you click the UI button? In that case I think the syntax problem is in the HTML that is added after the .solo1 element, specifically in the onclick attribute:

$('.solo1').after('<span ... onclick="function save (value)"</span><br/>')

That should be:

$('.solo1').after('<span ... onclick="save (value)"</span><br/>')

Because you want the function save to be called (not defined, hence the removal of the keyword function from the onclick attribute).

But looking at your code and reading your description, I think you want the value of the input element adjacent to the button to be passed into the function?

In that case a better solution would be to first create the UI buttons and add the click handler to each later:

// Create button and then add it after each input
var button = $('<span ... ></span><br/>');
$('.solo1').after(button);

// Now that the buttons are created, find them and give each a click handler
$('.saveButton').click(function() {
  // Get value from the input element that this button was added after
  var value = $(this).prev().val();

  // Now call the save method with the value
  save(value);
});

Working example: http://jsfiddle.net/h78hw/

∝单色的世界 2024-12-31 16:35:40

在这里运行 JSLint:http://jsfiddle.net/kp6hV/ 显示:

Error:
Problem at line 14 character 92: Duplicate member 'data'.
data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1...

Problem at line 19 character 10: Extra comma.
},

Implied global: $ 1,2,4,10,14, console 18

也就是说,有一个正如我上面评论的那样,“成功”函数后面有额外的逗号,以及重复的“数据”成员。

编辑:无错误:

function save(value) {
  $.ajax({
    url: "/*ServerURL*/",
    type: "POST",
    //data: '{ $("#craven1") : "' + value + '" }', // Not sure what's going on here
    data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1").val() + "}",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
    }
  });
}

Running JSLint on it here: http://jsfiddle.net/kp6hV/ reveals:

Error:
Problem at line 14 character 92: Duplicate member 'data'.
data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1...

Problem at line 19 character 10: Extra comma.
},

Implied global: $ 1,2,4,10,14, console 18

That is, there is an extra comma after your 'success' function, and duplicate 'data' members, as I've commented above.

Edit: Error free:

function save(value) {
  $.ajax({
    url: "/*ServerURL*/",
    type: "POST",
    //data: '{ $("#craven1") : "' + value + '" }', // Not sure what's going on here
    data: "{ 'Id': " + $("#craven1").text() + ", 'content': " + $(".solo1").val() + "}",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
    }
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文