我在PHP $idPost中有变量,它随机从数据库mysql中获取。
下面的JavaScript包含在while进程中。我希望我也能在JavaScript中得到乘法var
。但这似乎行不通。
这里是我的javascript基础onclick事件:
<img class="share" alt="1" src="comment.png" id="comment" onclick="myFunction()">
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("comment<?php echo $idPost;?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
//var idpost<?php echo $number;?> = document.getElementById("comment<?php echo $number;?>").alt;
var textarea<?php echo $idPost;?> = 'hai<?php echo $idPost;?>';
var a = "textarea";
var b = "<?php echo $idPost;?>";
var c = a+b;
document.getElementById(c).value=textarea<?php echo $idPost;?>;
// When the user clicks the button, open the modal
function myFunction() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
我的问题是在html:
<textarea class="form-control" rows="2" placeholder="Type Your Comment Here" name="content" id="textarea<?php echo $idpost;?>"></textarea>
每当我改变到id="textarea45"
(其中45是第一个idpost
)它的工作。但是当我使用$idpost
时,它不起作用。
我对两件事感到困惑。
JavaScript中的textarea值获取最后一个
$idpost
,但在textareaid
中获取第一个$idpost
。当我从
$idpost
更改为一些变量时,我自动递增,文本区域id
获得最后一个/最大的数字,JavaScript中的值获得第一个/最小的数字。
我如何使用这个代码?
尝试Jquery模式打开函数
$ (" # myModal ") .modal ();
你可以在按钮的onclick函数或任何地方使用它
关于php变量,您可以使用Ajax从php获取数据。更好的数据格式是JSON为您的项目