2013-12-12 172 views
0

所以,我有一个非常简单的添加和删除图片集。他们目前拥有单选按钮,可以让用户选择哪一个是默认图片。这里是一个简单的HTML为此:单选按钮被禁用后,下一个单选按钮被选中

<table> 
    <tr> 
     <td class="vatop aleft"> 
      <table>            
       <tr> 
        <td><div id="pic1" class="picture" ><span>1</span></div></td> 
        <td><div id="pic2" class="picture" ><span>2</span></div></td> 
        <td><div id="pic3" class="picture" ><span>3</span></div></td> 
        <td><div id="pic4" class="picture" ><span>4</span></div></td> 
        <td><div id="pic5" class="picture" ><span>5</span></div></td> 
       </tr> 
       <tr> 
        <!--JDO 12.12.2013 CWA-9448 --> 
        <td class="acenter"><input id="default1" type="radio" name="defaultpic" class="defaultpic" picnumber="1" /><span><!--This is where default text go--></span></td> 
        <td class="acenter"><input id="default2" type="radio" name="defaultpic" class="defaultpic" picnumber="2" disabled="disabled" /><span><!--This is where default text go--></span></td> 
        <td class="acenter"><input id="default3" type="radio" name="defaultpic" class="defaultpic" picnumber="3" disabled="disabled" /><span><!--This is where default text go--></span></td> 
        <td class="acenter"><input id="default4" type="radio" name="defaultpic" class="defaultpic" picnumber="4" disabled="disabled" /><span><!--This is where default text go--></span></td> 
        <td class="acenter"><input id="default5" type="radio" name="defaultpic" class="defaultpic" picnumber="5" disabled="disabled" /><span><!--This is where default text go--></span></td> 
      <!--END--> 
         </tr> 
       <tr> 
        <td class="aleft vabottom" colspan="5" style="padding-top: 15px;" > 
         <form id="uploadform" name="uploadForm" method="post" enctype="multipart/form-data" action="FileTransferHandler.ashx" target="pictureframe"> 
          <input id="filepicture" name="filepicture" type="file" class="file" style="margin-right: 5px; width: 450px;" /> 
          <select id="picturenumber" class="" style="margin-right: 5px; width: 80px;"></select> 
          <input id="uploadpicture" type="submit" value="{{UIT.[147743]}}" class="link" /> 
          <a id="deletepicture" href="javascript:void(0)" rel="deletepicture" class="link" >{{UIT.[8397]}}</a> 
         </form> 
         <iframe id="pictureframe" name="pictureframe" src="about:blank" class="hidden"></iframe> 
        </td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
</table> 

现在这个我有一个问题与删除默认图像。例如,我将上传三张图片,并选择第二张图片作为默认图片,然后删除该图片,图片被删除,但随着我的代码选择下一张默认图片(这是自从它有图片开始的第一张图片),它正在被选中但单选按钮未被检查。见我的代码片段进行删除:

var $deletepicture = $("#main-template #deletepicture"); 
$deletepicture.click(function() { 
    $("#main-template #picturenumber option:selected").each(function() { 
    _picnum = $(this).attr("code"); 
    //Remove the image, show the span, reset the filename attribute of the div 
    $("#pic" + _picnum.toString() + " img").remove(); 
    $("#pic" + _picnum.toString()).find("span").show(); 
    $("#pic" + _picnum.toString()).removeAttr("filename"); 
    $("#pic" + _picnum.toString()).hide().show(); 
    //Choose another default 
    $("#default" + _picnum.toString()).removeAttr('checked'); 
    $("#default" + _picnum.toString()).next().text(''); 
    $("#default" + _picnum.toString()).prop('disabled', true); 
    for (var i = 1; i < 6; i++) { 
     var $div = $("#pic" + i.toString()); 
     if ($div.attr("filename") !== undefined) { 

     $("#default" + i.toString()).attr('checked', 'checked'); 
     $("#default" + i.toString()).next().text(' Default'); 
     //JDO 12.12.2013 CWA-9437 
     return false; 
     } 
    }; 
}); 
}); 

我可以看到从第二图片的第一张照片的“默认”的举动,但我有一个很难让单选框进行检查。

回答

1

从jQuery的1.9起,激活单选按钮,使用方法:的

$("#default" + i.toString()).prop("checked", true); 

代替

$("#default" + i.toString()).attr('checked', 'checked'); 
+0

谢谢!那是一种解脱! – jellyfication

+0

如果这个答案对你来说是足够的,请将其标记为已接受的答案。如果你需要进一步的解释,请告诉我:D –

相关问题