简体   繁体   English

如何使表的列在jquery中每行仅占用两个单词?

[英]How to make a column of a table to take only two words per line in jquery?

I have made a table in html which is updated when a person chosse an item from the item list. 我用html制作了一个表格,当一个人从项目列表中选择一个项目时,该表格就会更新。 I want when a cell of the table is updated, it should only take two words of the selected item and if the selected item is of three words, then the third word should print on next line of the cell. 我想在更新表格的单元格时,只应使用所选项目的两个单词,如果所选项目为三个单词,则第三个单词应打印在该单元格的下一行。

This is html table from where an item is selected 这是从中选择项目的html表

<table>
   <tr>
      <td style="text-align:left;"><input type='checkbox' name='it's 2' value='Two Words'>Two words</input>
      </td>
      <td style="width:200px;"></td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
   <tr>
      <td style="text-align:left;">
          <input type='checkbox' name='this is three' value='Three Words' >Three words Next line</input>
      </td>
      <td style="width:200px;"></td>
      <td></td>
      <td></td>
      <td></td>
   </tr>
</table>

This is the html table which is updated... 这是已更新的html表...

<table border="1" class="table2">
   <tr>
      <td>Name</td>
      <td>Value</td>
   </tr>
   <tr>
      <td class="text"></td>
      <td class="value"></td>
   </tr>
</table>
</div>

I want that second element of first table when selected is printed on second table but in two lines, where first line will have "three words" and the second line will have "Next line" on the same cell of second table. 我希望第一张表的第二个元素被选中时打印在第二张表上,但要打印在两行中,其中第二行在第二张表的同一单元格上具有“三个单词”,第二行将具有“下一行”。 Pl help. 请帮助。

jQuery Code : jQuery代码:

$('input[type="checkbox"]').click(function(){
    if($(this).is(":checked")){
        var x = $(this).val();
        $('.text').append(x + ' <br />').show();
        var z = $(this).attr('name');
        $('.value').append(z + ' <br />');
    }
    else if($(this).is(":not(:checked)")){ 
        var x = $(this).val();
        var y = $('.text').text().replace(x, '');
        $('.text').text(y).show();
        $('.text').append('<br/>');
        var z = $(this).attr('name');
        var a = $('.value').text().replace(z, '');
        $('.value').text(a).show();
        $('.value').append(' <br/>');
    }
});

To capture first two words: var theTwoWords = yourText.match(/([a-zA-Z]+\\s+[a-zA-Z]+)/)[0]; 要捕获前两个单词: var theTwoWords = yourText.match(/([a-zA-Z]+\\s+[a-zA-Z]+)/)[0]; To get the next two after these: var theNextTwoWords = theTwoWords.substr(theTwoWords.length + 1); 要获得这些之后的下两个: var theNextTwoWords = theTwoWords.substr(theTwoWords.length + 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM