javascript - 单击 td 时转换为下拉框并将更改保存到数据库

标签 javascript php mysql

我有一个脚本,当单击 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 - 单击 td 时转换为下拉框并将更改保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573996/

相关文章:

javascript - 如何使用 nextjs 从 url 中删除/[lang 标签]?

php - 如何在多组数据集中搜索 mysql 查询中的列和值

php - 为什么JSON显示没有数据

PHP 日期计算器返回错误的日期

mysql - 带有引用 MySQL 中同一个表的子查询的 SQL UPDATE

javascript - 从 FBML 切换到 Javascript SDK 和社交插件

javascript - 无法将 prop 传递回父组件

mysql - 如何在 MySQL Workbench 中重置 root 用户的密码

MySQL日期格式更改

javascript - 使数据属性表现得像 anchor