[英]Get text inside <p> tag to a <table>
In PHP this is the output I managed to get from a calculation在 PHP 这是我设法从计算中得到的 output
<p id="generated">12345678199824,12345678411140,12345678921494,12345678497535,</p>
and I need this values to be as below我需要这个值如下
<table>
<tr>
<td>12345678199824</td>
</tr>
<tr>
<td>12345678411140</td>
</tr>
.....
</table>
Below is the js script I used下面是我使用的js脚本
$("#setVal").click(function(event){
event.preventDefault();
var noOfImei = parseInt($("#nofoemei").val());
var imeiList = "";
if (noOfImei !== 0) {
for (var i = 0; i < noOfImei; i++) {
imeiList += $("#binnumber").val() + Math.floor(Math.random()*900000+100000) + ",";
}
$("#generated").text("");
$("#generated").text(imeiList);
// testing purpose
console.log(imeiList);
} else {
alert("Please add no of IMEI(s) need to generate");
}
});
-- Please not Im a beginner to PHP and JS and managed to progress this far with help of stackoverflow and tutorials. -- 请不要我是 PHP 和 JS 的初学者,并在 stackoverflow 和教程的帮助下取得了进展。 Thank You.谢谢你。
$("#generated").text("");
$("#generated").text(imeiList);
data = imeiList.split(",")
var table = document.getElementById("myTable");
for(let i =0;i<data.length ; i++){
var row = table.insertRow(i);
var cell1 = row.insertCell(0);
cell1.innerHTML = data[i];
}
And和
<table id="myTable" /> // this in dom
you can get the related string and via split create an array.您可以获取相关字符串并通过拆分创建一个数组。 then add rows like follwing:然后添加如下行:
document.getElementById("setVal").addEventListener("click", function(){ let table = document.getElementById("myTable"); let nums = document.getElementById("generated").innerHTML; if(nums){ nums.split(',').forEach(i=> { let row = table.insertRow(0); let cell1 = row.insertCell(0); cell1.innerHTML = i; }) } });
<p id="generated"> 12345678199824,12345678411140,12345678921494,12345678497535,</p> <input type="button" id="setVal" value="Set Rows"/> <table id="myTable" style="border: 1px solid red;"> </table>
In terms of PHP initially you must explode your string and then by counting the string length it becomes more simpler to add a for
loop就 PHP 而言,最初您必须分解字符串,然后通过计算字符串长度,添加for
循环变得更加简单
<?php
$str = "12345678199824,12345678411140,12345678921494,12345678497535";
$str1 = explode(",",$str);
$arrlength=count($str1);
?>
<table style = "border:1px solid;border-collapse : collapse;">
<?php
for($x=0;$x<$arrlength;$x++)
{
?>
<tr style = "border:1px solid;">
<td style = "border:1px solid;"><?php echo $str1[$x]?></td>
<tr>
<?php } ?>
</table>
Note: Select the correct separator(in this example it is ' , ') in the explode()
function.注意: Select 在explode()
function 中是正确的分隔符(在这个例子中是' , ')。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.