2012-02-27 67 views
2

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

我已经把完整的代码JSFiddle,问题是,它获取第一个输入框val,但不获取内容checkboxchecked

的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(); 
     } 
    });    
}​ 
+0

将相关代码**置于问题**中。为什么:http://meta.stackexchange.com/questions/118392/add-stack-overfow-faq-entry-or-similar-forputing-code-in-the-question – 2012-02-27 08:23:51

+0

他们都有相同的ID,并且您试图获取该ID的值,因此它只取第一个ID,因为它不认为有更多ID,因为它应该是唯一的。 – adeneo 2012-02-27 08:30:01

回答

5

与此

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

更换你$.each功能,它应该工作。

但是我必须强调,然而,你听听上面的人说什么,以防止未来的错误发生。

+0

我试过我们的代码,它只是显示第一个输出.... – dude 2012-02-27 11:22:10

+0

@dude我可以捕获我检查的任何文本框的文本。这里是我所做的[jfiddle](http://jsfiddle.net/drewP/hrdDD/)。 – Drew 2012-02-27 11:31:32

+1

正确的一个!谢谢...这是我需要的扫管笏! – dude 2012-02-27 11:49:51

0

所以,很多事情错了...
每个复选框的name应该是唯一的,否则你应该把它作为一个组,name="group[]"。与id相同,它们应该是唯一的,并且您使用的许多id="phototitle"。这可能是您的代码无法正常工作的原因。当你想这样做

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

:那你在做这个

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

等等,等等......我怕你可能需要重新考虑整个事情。

+0

如何在js – dude 2012-02-27 08:44:54

+0

即ie中调用name = group []。所有带有'name = colors []'的复选框都将放在一个组中,然后你可以像使用'forEach(颜色在$ _POST ['colors']中)一样遍历'' – elclanrs 2012-02-27 09:10:55

+0

罚款将尝试让你知道 – dude 2012-02-27 09:18:01