我正在尝试使用 Ajax 提交数据X-可编辑 https://vitalets.github.io/x-editable/docs.html并且在运行 url 参数中定义的 php 脚本时遇到问题。实际上,我从工作中得到了基本的例子:
Html:
<a href="#" id="username" data-type="text" data-pk="1" data-name="username" data-original-title="Enter username" class="editable">superuser123</a>
Js:
$('#username').editable({
url: 'post.php',
type: 'text',
pk: 1,
name: 'username',
title: 'Enter username'
});
这是有效的(post.php
被执行)。但现在我想要更多的可编辑字段并在单击按钮时运行它们。这是我的 html(我使用的是 Smarty):
{foreach from=$categories key="category_key" item="category_item"}
<tr>
<th scope="row">{$category_key + 1}</th>
<td>
<span id="edit-button-{$category_key + 1}" data-pk="1" data-original-title="Edit category name" data-toggle="#edit">
{$category_item["name"]}
</span>
<button class="btn edit"><span class="glyphicon glyphicon-pencil"></span></button>
<td>0</td>
</tr>
{/foreach}
以及相关的Javascript:
$(document).ready(function() {
//toggle `popup` / `inline` mode
$.fn.editable.defaults.mode = 'inline';
$('.edit').click(function(e){
e.stopPropagation();
var button = $(this).prev('span').attr('id');
$('#'+button).editable('toggle',{
url: 'post.php',
type: 'text',
pk: 1,
name: button,
title: 'Edit category'
});
});
});
问题是,这会创建应有的可编辑字段,但它不会调用post.php
脚本(与第一个示例不同)。我做错了什么?