从html表格输入单元格点击

From html table to input on cell click

本文关键字:单元格 输入 html 表格      更新时间:2023-09-26

我有一个包含许多单元格的价格表,如果我点击单元格,我将获得警报中的"项目名称、项目数量和项目格式"等数据:

$(function() {
  $('#table').on('click', 'td', function(e) {
      var format = e.delegateTarget.tHead.rows[0].cells[this.cellIndex],
          quantity  = this.parentNode.cells[0],
          name = document.querySelectorAll("#item_name");
          alert([$(name).text(), $(quantity).text(), $(format).text()]);
  })
});

现在的问题是,我想将这些数据传递到我的联系人表单中的禁用输入字段中。但是我真的不知道该怎么做。我希望你能理解我的意思!表的Url为:mydoamin.com/catalog/item/1要联系的Url为:mydomain.com/contact

我的输入字段代码:

<div class="form-group">
<label for="subject" class="control-label">Bestellung</label>
<?php print form_error('order'); ?>
<?php print form_input('order', set_value('order'), 'placeholder="" class="form-control" id="disabledInput" disabled'); ?>
</div>

摆弄表格和JS代码:https://jsfiddle.net/0bof336t/1/

谢谢!

您可以使用$('#disabledInput').val(<value to insert>);插入要输入的值。在您的情况下,类似于以下内容:$('#disabledInput').val($(name).text());

$(function() {
      $('#table').on('click', 'td', function(e) {
          var format = e.delegateTarget.tHead.rows[0].cells[this.cellIndex],
              quantity  = this.parentNode.cells[0],
              name = document.querySelectorAll("#item_name");
                $('#disabledInput').val($(name).text());
              alert([$(name).text(), $(quantity).text(), $(format).text()]);
      })
    });

如果您的输入在另一个页面中,那么您应该通过get方法或cookie传递值。

通过GET方法

Javascript

$(function() {
      $('#table').on('click', 'td', function(e) {
          var format = e.delegateTarget.tHead.rows[0].cells[this.cellIndex],
              quantity  = this.parentNode.cells[0],
              name = document.querySelectorAll("#item_name");
                window.location.replace('url/?value='+$(name).text());
      })
    });

然后在你的php文件中应该是这样的:<?php print form_input('order', $_GET['value'], 'placeholder="" class="form-control" id="disabledInput" disabled'); ?>

通过COOKIE方法

Javascript

$(function() {
          $('#table').on('click', 'td', function(e) {
              var format = e.delegateTarget.tHead.rows[0].cells[this.cellIndex],
                  quantity  = this.parentNode.cells[0],
                  name = document.querySelectorAll("#item_name");
                  document.cookie = inputValue + "=" + $(name).text()+ "; " + 3600000 +"; path=/";
          })
        });

然后您可以使用php访问cookie并从中选择值。