简体   繁体   English

如何在 div 中选中单选框动态检查

[英]How to check dynamically with radio box is selected in the div

I need a dynamic function for checking whether a radio box is selected in the div or not.我需要一个动态 function 来检查是否在div中选择了radio box

Actually I want to show an error message every time an user, without selecting the radio button , presses the next button.实际上,我希望每次用户在不选择radio button的情况下按下下一个按钮时显示一条错误消息。

I am working on this page .我正在处理这个页面 The ILERI means NEXT . ILERI表示NEXT

These are the divs.这些是div。 There are 9 div on the page cited above.上面引用的页面上有 9 个 div。

<div id="div2" class="targetDiv">
    <a id="back"  class="ileri-geri" target="1"  >GERİ</a> <h1>Cep Çesitleri</h1>
    <a  class="ileri-geri" target="3">İLERİ</a>
    <div class="urunler">
        <ul class="urun">
            <li><img src="imagesBOX/items/cepsayisi/0.jpg"></li>
            <li><p>Cepsiz</p><input type="radio" name="cep_cesitleri" value="0" /></li>
            <li></li>
        </ul>

        <ul class="urun">
            <li><img src="imagesBOX/items/cepsayisi/1.jpg"></li>
            <li><p>Tek Cepli</p><input type="radio" name="cep_cesitleri" value="1" /></li>
            <li></li>
         </ul>

        <ul class="urun">
            <li><img src="imagesBOX/items/cepsayisi/2.jpg"></li>
            <li><p>Çift Cepli</p><input type="radio" name="cep_cesitleri" value="2" /></li>
            <li></li>
        </ul>

        <h1>Cep Sayisi</h1><br/>


        <?php foreach($Secondresult As $jcRow):  ?>
                <ul class="urun">
                <li><img src="imagesBOX/items/cep_cesitleri/<?php echo $jcRow->image ?>" /></li>
                <li><p><?php echo $jcRow->title ?></p><input type="radio" name="cep_cesitleri" value="<?php echo $jcRow->id ?>" /></li>
                <li></li>
                </ul>
          <?php endforeach; ?>


        </div>



    </div>
   <!----2nd page--->

this is javascript which hides all the div and show the div only based on target attribute of tag.这是 javascript 隐藏所有 div 并仅根据标签的目标属性显示 div。

<script type="text/javascript">
    $(document).ready(function(){                          
        $('.targetDiv').hide();
        $('#divfirst').show();
        $(".ileri-geri").click(function(event){
            $('.targetDiv').hide();
            $('#div'+$(this).attr('target')).fadeIn('slow');
            //alert("Redirecting you to jQuery.com!");
        });
</script>

Now I want that, If user click on ILERI button without selecting the radio option, then the error mesg will appear, but the function should dynamically show that it handles all the 9 div on this page.现在我想要这样,如果用户在不选择单选选项的情况下单击ILERI按钮,则会出现错误消息,但 function 应动态显示它处理此页面上的所有 9 个 div。

You can check if at least one item was checked with this:您可以检查是否至少检查了一项:

$(".urun input:checked").length() > 0
$(".ileri-geri").click(function(event){
    //you have to detect the visable .targetDiv first
    if($('.targetDiv:visible input:checked').length() > 0) {
        $('.targetDiv').hide();
        $('#div'+$(this).attr('target')).fadeIn('slow');
    }else{
        alert("Error");
    }
});

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

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