我正在尝试通过查询和ajax更新表中的一行。
我成功了,但是我有一个问题。
我所拥有的页面是一种只有一个输入的表单;输入是id
数据库中该行的。提交表单后,我会更新该行,但是如果我id
在输入中插入另一行,则该行将无法工作,直到再次刷新页面为止。含义:我必须再次刷新页面以更新表中的另一行。我正在使用codeigniter。
这是我的代码:
<form method="POST" action="" >
<fieldset data-role="controlgroup">
<input type="text" name="id" id="id" value=""
<input type="submit" name="submit-choice-a1" id="submit-choice-b1" value="submit" />
</fieldset>
</form>
而JS是:
<script type="text/javascript">
$(function() {
$("#submit-choice-b1").click(function(e) {
var id = $('#id').val();
var result = confirm("are you sure ?");
if (result==true) {
$.ajax({
url: "the url that have the page with the update function",
success: function(xhr){
alert('updated');
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
}
});
}
});
});
</script>
我看到2种方式:
1)不要使用<form>
标签,只需获取信息并使用jQuery ajax将其发送到data参数中即可。
$.ajax({
url: "the url",
type: "post",
data: {
id: 68,//Your field id/class, got it with $(selector).attr("id") or $(selector).attr("class");
anotherKey: anotherValue
etc...
}
});
2)使用onsubmit属性修改表单
<form method="post" onsubmit="return sent();">
send()函数的末尾,您需要添加“ return false”以告知表单(不要重新加载页面)
function sent() {
..your ajax implementation here..
return false;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句