使用jquery查找最接近复选框的输入类型文本的值

时间:2012-02-27 08:21:38

标签: jquery

Working code in JSFIDDLE

我已将完整的代码放在JSFiddle中,问题是它获取了第一个输入框的val,但是没有获取checkboxchecked的内容{1}}?

HTML

<div class="gallery_content_wrap">
<!-- video wrap-->

<div class="gallery_photo_wrap">
  <div class="gallery_bg_wrap">
  <input type="hidden" value="10" name="selectypeaccept">
  <input type="hidden" value="photos" name="subadmintypeaccept">

        <div name="photosacceptdiv" class="gallery_bg"> <img width="200px" border="0" height="148px" src="http://farm8.staticflickr.com/7180/6846918679_7891edd209_t.jpg">  
<br>   <input type="text" value="" id="phototitle">  <input type="checkbox" value="1d7457596c0f202ae373de1737bb0e4a.png" name="photosaccept">
      </div>

  </div>
</div>
<!-- video wrap-->

<div class="gallery_photo_wrap">
  <div class="gallery_bg_wrap">
  <input type="hidden" value="10" name="selectypeaccept">
  <input type="hidden" value="photos" name="subadmintypeaccept">

        <div name="photosacceptdiv" class="gallery_bg"> <img width="200px" border="0" height="148px" src="http://farm7.staticflickr.com/6186/6054843777_4e90c30827_t.jpg">  
<br>   <input type="text" value="" id="phototitle">  <input type="checkbox" value="3.png" name="photosaccept">
      </div>

  </div>
</div>
<!-- video wrap-->

<div class="gallery_photo_wrap">
  <div class="gallery_bg_wrap">
  <input type="hidden" value="10" name="selectypeaccept">
  <input type="hidden" value="photos" name="subadmintypeaccept">

        <div name="photosacceptdiv" class="gallery_bg"> <img width="200px" border="0" height="148px" src="http://farm8.staticflickr.com/7021/6846957961_779518a6c5_t.jpg">  
<br>   <input type="text" value="" id="phototitle">  <input type="checkbox" value="9d0757dd339b0b1afa3aabe6b1a50cda.png" name="photosaccept">
      </div>

  </div>
</div>
<!-- video wrap-->    

<div class="gallery_photo_wrap">
  <div class="gallery_bg_wrap">
  <input type="hidden" value="10" name="selectypeaccept">
  <input type="hidden" value="photos" name="subadmintypeaccept">

        <div name="photosacceptdiv" class="gallery_bg"> <img width="200px" border="0" height="148px" src="http://farm8.staticflickr.com/7181/6858152461_91dd47bfc8_t.jpg">  
<br>   <input type="text" value="" id="phototitle">  <input type="checkbox" value="26a70bb20de5d595d2fde46abbc740f8.png" name="photosaccept">
      </div>

  </div>
</div>
<!-- video wrap-->            

</div><div align="center"><input type="submit" name="submitaccept" value="Approve"/><input type="submit" name="submitacceptreload" value="Reload"/>
</div>

Jquery的

$("[name=submitaccept]").click(function(){
    checktype();
});

function checktype(){
    $("[name=photosaccept]").each(function() {       
        if ($(this).is(':checked')) {
            $phototitle=$(this).val();
            alert($("#phototitle").val());
            $photoname = $("#phototitle").val();
        }
    });            
}​

2 个答案:

答案 0 :(得分:5)

用此

替换您的$.each功能
$("[type=checkbox]").each(function() {
    if($(this).is(':checked')){
        alert($(this).closest("div.gallery_bg").find("input[type=text]").val());
    }
});

它应该有效。

但是我必须强调,你要听听上面的人说的话,以防止将来发生错误。

答案 1 :(得分:0)

这么多错了...... 每个复选框的name应该是唯一的,否则您应该将其作为一个组name="group[]"。与id相同,它们应该是唯一的,并且您使用了许多id="phototitle"。这可能是您的代码无法正常运行的原因。那你就是这样做的:

$photoname = $("[name=phototitle]").val();

如果你想这样做:

$photoname = $("#phototitle").val();

等等......我担心你可能需要重新考虑整件事。