javascript - 单击 td 时转换为下拉框并将更改保存到数据库
问题描述
我有一个脚本,当单击 td(表数据)时会显示一个下拉框。它包含选项“X”和“O”。
我的 html 表包含我的数据库中的数据。我的客户想要对数据进行前端编辑,因为这对他们来说很容易。所以我将 td 转换为下拉列表,以便他们可以轻松地发表评论。但是如何将用户在表中所做的更改保存到数据库中。
这是使 td 成为下拉框的脚本:
$(document).on('click', 'td', function() { ////---make td transform to dropdown list box when click---///
if($(this).find('select').length == 0) {
$(this).empty(); //clears out current text in the table
$(this).append('<select><option></option><option style="font-size:20px;">X <option style="font-size:20px;">O</select>');
}
});
解决方案
将名称和类别添加到您的选择框
$(document).on('click', 'td', function() {
if($(this).find('select').length == 0)
{
$(this).empty(); //clears out current text in the table
$(this).append('<select class="selectbox" name="selectbox"><option></option><option
style="font-size:20px;" value="X">X <option value="0" style="font-size:20px;">O</select>');
}
});
并创建一个 jquery 更改事件,尝试这样的事情
$(document).on('change', '.selectbox', function() {
$.ajax({
type: 'POST',
crossDomain: true,
data: {
data_value: this.value,
},
url: 'your_url_to_update_to_db.php',
success: function (data)
{
//do Something
},
});
});
推荐阅读
- javascript - 如何将带有索引元素的函数向下传递到组件?
- java - 解释 if 块嵌套
- python - 为会话 AI 微调 GPT-2 文本预测
- sql - 数组中对象的自动增量 id
- android - 类型错误:未定义不是对象(评估'style.inputStyle')
- javascript - 使用 toDataURL 在画布上绘制图像
- random - Roblox 脚本 - 随机陷阱激活
- javascript - ng build --prod 不生成 dist 文件夹(角度 7)
- regex - 正则表达式查找具有起始词和结束词的最短字符串
- node.js - 如何将'semantic-ui'应用到我的 node.js 项目?node.js 视图由 pug 文件组成