请考虑以下代码段:
<a href="profile.php?viewuserprofile=45">
<img style="width: 15% !important;" class="tableimg" src="core/uploads/321344.jpg" alt="profilename">
</a>
<br><br>
<textarea id="chatMessage2" name="chatMessage" style=" padding: 2%; width: 100% !important; background:#d4d4d4; border:0;" placeholder="Type your message here... " value=""></textarea>
<button id="buttons2" type="submit" name="sendChat" class="btn btn-default pull-right">Send</button>
<input id="fromUserId2" name="fromUserId" value="45" type="hidden">
<input id="toUserId2" name="toUserId" value="45" type="hidden">
如何使动态ID可用于jQuery?我使用php while语句动态生成所有内容。基本上所有的ID都是动态的。
EDIT FULL JS
<script>
$(document).ready(function(){
$("#buttons").click(function(){
var fromuserid = $("#fromUserId").val();
var touserid = $("#toUserId").val();
var chatMessage = $("#chatMessage").val();
// stored in database.
var dataString = 'fromUserId='+ fromuserid + '&toUserId='+ touserid + '&chatMessage='+ chatMessage;
// AJAX
$.ajax({
type: "POST",
url: "chat.php",
data: dataString,
cache: true,
success: function(response){
$("#displayMessage").html(response);
$("#chatForm").trigger("reset");
}
});
return false;
});
});
$(document).ready(function(e){ $.ajaxSetup({cache:false});
setInterval(function(){$('#displayMessage').load('log.php');}, 3000);
});
</script>
答案 0 :(得分:1)
$(this)
此上下文获取与点击按钮相关的信息下面的简单演示:
$('.buttons').click(function() {
var text = $(this).closest('.row').find('textarea.chatMessage').val();
var fromUserId = $(this).closest('.row').find('input.fromUserId').val();
var toUserId = $(this).closest('.row').find('input.toUserId').val();
var q = text + " fromUserId = " + fromUserId + " toUserId = " + toUserId
$(this).closest('.row').find('div.displayMessage').text(q)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='row'>
<textarea class="chatMessage" name="chatMessage" style=" padding: 2%; width: 100% !important; background:#d4d4d4; border:0;" placeholder="Type your message here... " value=""></textarea>
<button class="buttons" type="submit" name="sendChat" class="btn btn-default pull-right">Send</button>
<input class="fromUserId" name="fromUserId" value="45" type="hidden">
<input class="toUserId" name="toUserId" value="45" type="hidden">
<div class="displayMessage" style="">
</div>
</div>
<div class='row'>
<textarea class="chatMessage" name="chatMessage" style=" padding: 2%; width: 100% !important; background:#d4d4d4; border:0;" placeholder="Type your message here... " value=""></textarea>
<button class="buttons" type="submit" name="sendChat" class="btn btn-default pull-right">Send</button>
<input class="fromUserId" name="fromUserId" value="46" type="hidden">
<input class="toUserId" name="toUserId" value="46" type="hidden">
<div class="displayMessage" style="">
</div>
</div>
<div class='row'>
<textarea class="chatMessage" name="chatMessage" style=" padding: 2%; width: 100% !important; background:#d4d4d4; border:0;" placeholder="Type your message here... " value=""></textarea>
<button class="buttons" type="submit" name="sendChat" class="btn btn-default pull-right">Send</button>
<input class="fromUserId" name="fromUserId" value="47" type="hidden">
<input class="toUserId" name="toUserId" value="47" type="hidden">
<div class="displayMessage" style="">
</div>
</div>