Jquery - 如何从中选择下一个元素
Jquery - How to select the next element from this?
我正在从数据库中检索 9 个图像缩略图,每个缩略图都驻留在自己的div 中,如下所示:
<div class="post">
<div class="post_desc"></div>
<div class="post_title"></div>
<a href="#" class="linktopost">**<img src="img/thumb.png" class="thumb-img"/>**</a>
<form>
**<input type="hidden" class="postid" value="'.$post_id.'" />**
</form>
</div>
使用 Jquery,我正在尝试在单击缩略图时发布隐藏输入字段中的值,但我正在努力正确选择它。输入中的值随 9 个图像中的每一个而变化。
这就是我在Jquery上取得的成就:
$(".thumb-img").click(function(){
$.post("inc/fullpost.php", {postid: ##########.val()},
function(output){
$("#gotpostid").html(output).show();
}).fail(function(x,y,z){
$("#gotpostid").html(x + "<br />" + y + "<br />" + z)
});
});
那么,如何正确选择与图像缩略图位于同一包含类中的输入字段中的值呢?
您可以使用数据而不是hidden
元素。删除hidden
元素,然后尝试如下:
标记:
<img src="img/thumb.png" class="thumb-img" data-postid="'.$post_id.'"/>
脚本:
$(".thumb-img").click(function(){
$.post("inc/fullpost.php", {postid: $(this).data('postid')},function(output){
$("#gotpostid").html(output).show();
}).fail(function(x,y,z){
$("#gotpostid").html(x + "<br />" + y + "<br />" + z)
});
});
试试这个:
$(".thumb-img").click(function(){
var postId = $(this).closest(".post").find(".postid").val();
$.post("inc/fullpost.php", {postid: postId},
function(output){
$("#gotpostid").html(output).show();
}).fail(function(x,y,z){
$("#gotpostid").html(x + "<br />" + y + "<br />" + z)
});
});
$(".linktopost").click(
function(){ var ValueToPost= $(this).next("input").val();
$.post("inc/fullpost.php", {postid: ValueToPost},
function(output){
$("#gotpostid").attr('type', 'text').val(output);
}).fail(function(x,y,z){
$("#gotpostid").val(x + "<br />" + y + "<br />" + z)
});
});
尝试以下操作
$(yourImage).parent().find('.postid').get(0)
好吧,我不知道您是否意识到,但是您正在做的不是选择一个值,而是实际上是在 #gotpostid 元素中设置值。
要选择值,请采用以下方法:
$value = $('#gotpostid').val();
如果要设置它,则:
$('#gotpostid').val($value);
相关文章:
- 使用jQuery选择下一个选项
- 角度 - ng 网格通过分组按语法选择下一个/上一个项目
- Jquery - 如何从中选择下一个元素
- Javascript幻灯片选择“下一个”图像混淆
- 如何使用 jquery 选择下一个元素
- jQuery 按类选择下一个同级,其中一个类包含多个类
- 使用 jquery 仅选择下一个
- 使用jquery按回车键选择下一个字段
- EXTJS 4.1如何在树面板中选择下一个叶节点
- Jquery选择下一个类
- 从选定的单选按钮中选择下一个单选按钮
- 选择下一个列表组并更改数据状态
- 选择下一个元素轮播
- 如何为Rails 3.1.3选择下一个CSS3/HTML5/JS/GRID框架
- Selenium IDE:如何从日期选择器中选择下一个可用日期
- 如何在css选择器中选择下一个直接元素
- 为什么我不能使用纯JavaScript选择下一个DIV兄弟?
- 如何选择下一个特定数量的元素在循环中通过每次点击在纯javascript
- 通过jQuery逐个选择下一个元素
- jQuery 在匹配的单选按钮中选择下一个单选按钮