Ajax 将值从文本框保存到服务器
刚刚发现 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单击 UI 按钮时是否收到错误消息?在这种情况下,我认为语法问题出在 .solo1 元素之后添加的 HTML 中,特别是在 onclick 属性中:
应该是:
因为您希望调用函数 save (未定义,因此删除了关键字来自
onclick
属性的function
)。但是查看您的代码并阅读您的描述,我认为您希望将与按钮相邻的输入元素的值传递到函数中?
在这种情况下,更好的解决方案是首先创建 UI 按钮,然后为每个按钮添加单击处理程序:
工作示例: 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:
That should be:
Because you want the function save to be called (not defined, hence the removal of the keyword
function
from theonclick
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:
Working example: http://jsfiddle.net/h78hw/
在这里运行 JSLint:http://jsfiddle.net/kp6hV/ 显示:
也就是说,有一个正如我上面评论的那样,“成功”函数后面有额外的逗号,以及重复的“数据”成员。
编辑:无错误:
Running JSLint on it here: http://jsfiddle.net/kp6hV/ reveals:
That is, there is an extra comma after your 'success' function, and duplicate 'data' members, as I've commented above.
Edit: Error free: