简体   繁体   English

如何获得img jquery旁边最接近的输入值?

[英]How can I get the closest input value next to a img jquery?

I want to get the closest input value when user click the img in jquery, how can I do that? 当用户单击jquery中的img时,我想获得最接近的输入值,该怎么办? For instance, if user click img a, it will get the value'1'. 例如,如果用户单击img a,它将获得值“ 1”。

 <img src="">a</img> <input type="hidden" value="1" /> <img src="">b</img> <input type="hidden" value="2" /> <img src="">c</img> <input type="hidden" value="3" /> <img src="">d</img> <input type="hidden" value="4" /> <img src="">e</img> 

Use next() 使用next()

 $('img').click(function() { var value = $(this).next().val(); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <img src="" alt=a /> <input type="hidden" value="1" /> <img src="" alt=b /> <input type="hidden" value="2" /> <img src="" alt=c /> <input type="hidden" class=a value="3" /> <img src="" alt=d /> <input type="hidden" value="4" /> <img src="" alt=e /> 

Update: If there is any other elements after the input then you need to use nextAll() and first() 更新:如果输入后还有其他元素,则需要使用nextAll()first()

 $('img').click(function() { var value = $(this).nextAll('input').first().val(); // you can avoid first(), since val() return value of first element from selector // var value = $(this).nextAll('input').val(); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <img src="" alt=a /> <input type="hidden" value="1" /> <span>hello</span> <img src="" alt=b /> <input type="hidden" value="2" /> <span>hello</span> <img src="" alt=c /> <input type="hidden" value="3" /> <span>hello</span> <img src="" alt=d /> <input type="hidden" value="4" /> <img src="" alt=e /> 

try this 尝试这个

$('img').click(function() {
  var value = $(this).next().val();
});
$("img").click(function(){
   alert($(this).parents('.input-group').find('input').val());
});

Hope this will help you !! 希望这个能对您有所帮助 !!

There will be img tags other than these in your page, I guess. 我想您的页面中将有除这些标签之外的其他img标签。 in that case it will be better if you can surround these img tags with a div and do something like below. 在这种情况下,最好将这些img标签用div括起来并做如下操作。

<div id="parentDiv">
    <img src="">a</img>
    <input type="hidden" value="1" />
    <img src="">b</img>
    <input type="hidden" value="2" />
    <img src="">c</img>
    <input type="hidden" value="3" />
    <img src="">d</img>
    <input type="hidden" value="4" />
    <img src="">e</img>
</div>

and the jquery you should put is, 你应该放的jQuery是

$("#parentDiv img").each(function(){

    $(this).click(function(){

        if($(this).next().val()){

            alert($(this).next().val());
        }
    });
});

if your page doesn't have any other img tags other than these just keep the html as it is and put this peace of code 如果您的网页除这些标签之外没有其他img标记,则只需保留html并放置此代码即可

$("img").click(function(){

    if($(this).next().val()){

        alert($(this).next().val());
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM