简体   繁体   English

加载照片数组时,如何为每个imageID创建唯一的注释按钮功能?

[英]When loading an array of photos, how can I make a unique comment button function for each imageID?

  1. Page loads with reference to commentIMage.js, and comment.php 页面加载参考commentIMage.js和comment.php
  2. User enters comment and clicks button, references from commentImage.js 用户输入注释并单击按钮,来自commentImage.js的引用
  3. comentImage.js ($comment).click(function{..}) doesn't load comentImage.js($ comment).click(function {..})不加载

html HTML

<textarea required=required name ="comment" id="comment"></textarea><br/>
              <input type="hidden" id="image" name ="image" value="<?php echo $images[$i]['imageID']?>" />
              <input type=button id=postComment value="Post Comment">

commentImage.js $(document).ready(function(){ commentImage.js $(document).ready(function(){

    $('#postComment').click(function(){
        $.ajax({
            type:'post',
            url:'commentImage.php',
            data:{newComment:$('#comment').val(), postID:postID},
            success:function(data){
                var data = JSON.parse(data);
                var comment = makeComment(data['user'], $('#comment').val(), data['date']);
                $('#commentsBock').prepend(comment);
                $('#comment').val('');
            }
        })
    })
})

function makeComment(user, comment, date){
    var comment = '<div><div>'+user+'</div><div>'+comment+'</div><div>'+date+'</div><div>';
    return comment;
}
function getComments(){

}

commentImage.php commentImage.php

<?php
session_start();
echo $_POST['comment'];
echo $_POST['image'];
include_once('mysql.php');
...
?>

#comment is a unique ID. #comment是一个唯一的ID。 If you use it more than one time in your HTML, don't wait for it to work properly, it won't ! 如果您在HTML中多次使用它,请不要等待它正常工作,它不会!

Just use your PostID like id="comment_"+PostID to create unique identifiers. 只需使用您的PostID,如id="comment_"+PostID即可创建唯一标识符。 And use $("input[id^='comment_']") to select all IDs which id start with comment_ (that's what ^= does) 并使用$("input[id^='comment_']")来选择id以comment_开头的所有ID (这就是^=做的)

JS Fiddle JS小提琴

暂无
暂无

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

相关问题 只要执行 function ,如何让按钮成为加载按钮? - How can I make a button a loading button as long as the function is executed? JavaScript-我可以使数组中的每个图像成为唯一链接 - JavaScript - Can I make each image in an array a unique link 如何在我的函数参数中使对象数组调用每个对象的函数? - How can I make an array of object in my function parameter call a function for each object? 如何为数组中的每个对象添加唯一 ID? - How can I add a unique ID to each object in an array? 如何用猫鼬制作评论模型? - How can I make comment model with mongoose? 如何在更改值而不是单击按钮时运行函数? - How can I make my function run when a value is changed instead of when the button is clicked? 如何为表格中的每一行制作一个接受按钮? - How can I make an accept button for each row in the table? Javascript未定义数组值未连接到函数? 如何运行随机特征函数以使每个数组包含唯一特征? - Javascript Undefined Array values not connecting to function? How to run a randomized trait function to make each array contain unique traits? 如何制作在页面加载时执行的功能? - How can I make a function which executes on page loading? 如何为数组中另一个数组中的对象中的每个对象分配唯一键和线性键? - How can I assign unique and linear key for each object within an array that's in an object within another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM