带有单选按钮的html表复制粘贴到xls中

时间:2012-09-04 07:17:14

标签: javascript html excel

我有一个HTML table元素,其中的行不仅包含纯文本字符,还包含radio buttonslists等。

我想要的是,当用户将表的内容复制粘贴到MS Excel(作为纯文本)时,radio buttons应该替换为它们的值(例如:“选中”,和“未选中”),列表元素应替换为其选定的值等。

有没有办法在浏览器中实现这个? (最好不使用flash或java applet组件)

谢谢, krisy

2 个答案:

答案 0 :(得分:2)

您可以使用javascript(使用jquery等框架更容易)来修改已复制的代码。一般程序是将单选按钮设置为不可见,然后根据其当前值在其位置添加文本。

有很多问题可以帮助您了解实施细节:https://stackoverflow.com/search?q=%5Bjavascript%5D+copied+text&submit=search

更新:原来你甚至不需要删除单选按钮,至少对于我的excel版本。这是一个工作示例(过度评论以解释发生了什么):

<!DOCTYPE html>
<html>
  <head>
    <style type='text/css'>
      .radioCopyVal{
      font-size: 0;
      }
    </style>
  </head>
  <body>
    <form>
      <table>
    <tr>
      <td><input type='radio' name='woots' id='oneWoot' checked/><span class='radioCopyVal'>Checked.</span></td>
      <td><label for='oneWoot'>Woot.</label></td>
    </tr>
    <tr>
      <td><input type='radio' name='woots' id='twoWoot' /><span class='radioCopyVal'>Not checked.</span></td>
      <td><label for='twoWoot'>Woot. Woot.</label></td>
    </tr>
      </table>
    </form>
    <script src="jquery-1.7.2.min.js"></script>
    <script type='text/javascript'>
      $("#oneWoot").attr("checked", true); //on page load, make sure oneWoot is the default selection

      $("input[name=woots]:radio").change(function(){ //anytime the radio buttons of name woots are changed...
          $(".radioCopyVal").remove(); //remove the old radioCopyVal spans (to be replaced with new ones)
          $("input[name=woots]:radio").each(function() { //for each radio button in name woots
              if ($(this).attr("checked")) { //if the button is checked, add a span saying 'Checked.' after it. css is used to hide the span
                  $(this).after('<span class="radioCopyVal">Checked.</span>');
          }else {
              $(this).after('<span class="radioCopyVal">Not checked.</span>');
          }
          })
      });
    </script>
  </body>
</html>

答案 1 :(得分:1)

不可能我害怕。但你可以这样做: 有一个导致javascript的链接,脚本将隐藏这些项目并以纯文本显示其值,以便可以撤消此操作。

这可以使用JQuery轻松完成: 这显示了无线电值:

$("input:radio").each(function() {
    if ($(this).attr("checked")) {
        $(this).after('<span class="value">checked</span>');
    }
    else {
        $(this).after('<span class="value">unchecked</span>');
    }
}).css("display", "none");

我留下了一个span类,以便可以使用脚本轻松隐藏这些值。