jQuery识别错误的ID

时间:2016-04-23 19:12:55

标签: jquery

我有问题,jQuery识别错误的ID。当用户更改他们的seks(男性或女性)时,jQuery会将新ID设置为四个元素(请参阅下面的代码)。 问题是,当我选择女性时,jQuery会认出我是男性。也许jQuery会看到ID在页面加载时有哪些元素。

四个元素

                                    <div class="btn-group _hsync_reg_sex" data-toggle="buttons">
                                    <br />
                                    <?php
                                        if($_hsync_rasa == 0)
                                        {
                                        ?>
                                            <label class="btn btn-default active" id="_hsync_bijelac_m">
                                                <input type="radio" name="_hsync_rasa" id="bijelac" autocomplete="off" checked> Bijelac
                                            </label>
                                        <?php 
                                        }
                                        else
                                        {
                                            ?>
                                            <label class="btn btn-default" id="_hsync_bijelac_m">
                                                <input type="radio" name="_hsync_rasa" id="bijelac" autocomplete="off"> Bijelac
                                            </label>                                                
                                            <?php
                                        }
                                    ?>
                                    <?php
                                        if($_hsync_rasa == 1)
                                        {
                                        ?>
                                            <label class="btn btn-default active" id="_hsync_crnac_m">
                                                <input type="radio" name="_hsync_rasa" id="crnac" autocomplete="off" checked> Crnac
                                            </label>
                                        <?php 
                                        }
                                        else
                                        {
                                            ?>
                                            <label class="btn btn-default" id="_hsync_crnac_m">
                                                <input type="radio" name="_hsync_rasa" id="crnac" autocomplete="off"> Crnac
                                            </label>                                                
                                            <?php
                                        }
                                    ?>
                                    <?php
                                        if($_hsync_rasa == 2)
                                        {
                                        ?>
                                            <label class="btn btn-default active" id="_hsync_hispanac_m">
                                                <input type="radio" name="_hsync_rasa" id="hispanac" autocomplete="off" checked> Hispanac
                                            </label>
                                        <?php 
                                        }
                                        else
                                        {
                                            ?>
                                            <label class="btn btn-default" id="_hsync_hispanac_m">
                                                <input type="radio" name="_hsync_rasa" id="hispanac" autocomplete="off"> Hispanac
                                            </label>                                                
                                            <?php
                                        }
                                    ?>
                                    <?php
                                        if($_hsync_rasa == 3)
                                        {
                                        ?>
                                            <label class="btn btn-default active" id="_hsync_azijac_m">
                                                <input type="radio" name="_hsync_rasa" id="azijac" autocomplete="off" checked> Azijac
                                            </label>
                                        <?php 
                                        }
                                        else
                                        {
                                            ?>
                                            <label class="btn btn-default" id="_hsync_azijac_m">
                                                <input type="radio" name="_hsync_rasa" id="azijac" autocomplete="off"> Azijac
                                            </label>                                                
                                            <?php
                                        }
                                    ?>                                      
                                </div>

jQuery代码(当用户改变性别时)

            $("#_hsync_musko").click(function() {
            var _rasa = Math.floor((Math.random() * 3) + 1);
            $('#_hsync_trenutna_rasa').attr("value",_rasa);

            if(_rasa == 0)
            {
                $('#_hsync_skin_dio_bijelac_m').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_muski_skinB; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinB; ?>.png");
            }
            if(_rasa == 1)
            {
                $('#_hsync_skin_dio_crnac_m').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_muski_skinC; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinC; ?>.png");
            }
            if(_rasa == 2)
            {
                $('#_hsync_skin_dio_hispanac_m').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_muski_skinH; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinH; ?>.png");
            }
            if(_rasa == 3)
            {
                $('#_hsync_skin_dio_azijac_m').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_muski_skinA; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinA; ?>.png");
            }                   

            $('#_hsync_bijelac_z').attr("id","_hsync_bijelac_m");
            $('#_hsync_crnac_z').attr("id","_hsync_crnac_m");
            $('#_hsync_hispanac_z').attr("id","_hsync_hispanac_m");
            $('#_hsync_azijac_z').attr("id","_hsync_azijac_m");

            $('#_hsync_skin_dio_bijelac_z').hide();
            $('#_hsync_skin_dio_crnac_z').hide();
            $('#_hsync_skin_dio_hispanac_z').hide();
            $('#_hsync_skin_dio_azijac_z').hide();  

            _hsync_prilagodi_rasa_tipke(0, _rasa);
        }); 

        $("#_hsync_zensko").click(function() {
            var _rasa = Math.floor((Math.random() * 3) + 1);
            $('#_hsync_trenutna_rasa').attr("value",_rasa);

            if(_rasa == 0)
            {
                $('#_hsync_skin_dio_bijelac_z').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_zenski_skinB; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinB; ?>.png");
            }
            if(_rasa == 1)
            {
                $('#_hsync_skin_dio_crnac_z').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_zenski_skinC; ?>");  
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinC; ?>.png");
            }
            if(_rasa == 2)
            {
                $('#_hsync_skin_dio_hispanac_z').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_zenski_skinH; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinH; ?>.png");
            }
            if(_rasa == 3)
            {
                $('#_hsync_skin_dio_azijac_z').show();
                $('#_hsync_trenutni_skin_id').attr("value","<?php echo $_hsync_zenski_skinA; ?>");
                $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinA; ?>.png");
            }

            $('#_hsync_bijelac_m').attr("id","_hsync_bijelac_z");
            $('#_hsync_crnac_m').attr("id","_hsync_crnac_z");
            $('#_hsync_hispanac_m').attr("id","_hsync_hispanac_z");
            $('#_hsync_azijac_m').attr("id","_hsync_azijac_z");

            $('#_hsync_skin_dio_bijelac_m').hide();
            $('#_hsync_skin_dio_crnac_m').hide();
            $('#_hsync_skin_dio_hispanac_m').hide();
            $('#_hsync_skin_dio_azijac_m').hide();

            _hsync_prilagodi_rasa_tipke(1, _rasa);              
        });     

当我点击比赛按钮

            $("#_hsync_bijelac_m").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","0");
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinB; ?>.png");
            $('#_hsync_skin_dio_bijelac_m').show();
            $('#_hsync_skin_dio_crnac_m').hide();
            $('#_hsync_skin_dio_hispanac_m').hide();
            $('#_hsync_skin_dio_azijac_m').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_muski_skinB; ?>);
            $('#skin_slika_<?php echo $_hsync_muski_skinB; ?>').attr("class","img-circle _hsync_reg_img_c");    
        }); 

        $("#_hsync_crnac_m").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","1");
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinC; ?>.png");
            $('#_hsync_skin_dio_bijelac_m').hide();
            $('#_hsync_skin_dio_crnac_m').show();
            $('#_hsync_skin_dio_hispanac_m').hide();
            $('#_hsync_skin_dio_azijac_m').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_muski_skinC; ?>);
            $('#skin_slika_<?php echo $_hsync_muski_skinC; ?>').attr("class","img-circle _hsync_reg_img_c");
        }); 

        $("#_hsync_hispanac_m").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","2");               
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinH; ?>.png");
            $('#_hsync_skin_dio_bijelac_m').hide();
            $('#_hsync_skin_dio_crnac_m').hide();
            $('#_hsync_skin_dio_hispanac_m').show();
            $('#_hsync_skin_dio_azijac_m').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_muski_skinH; ?>);
            $('#skin_slika_<?php echo $_hsync_muski_skinH; ?>').attr("class","img-circle _hsync_reg_img_c");
        }); 

        $("#_hsync_azijac_m").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","3");
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_muski_skinA; ?>.png");
            $('#_hsync_skin_dio_bijelac_m').hide();
            $('#_hsync_skin_dio_crnac_m').hide();
            $('#_hsync_skin_dio_hispanac_m').hide();
            $('#_hsync_skin_dio_azijac_m').show();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_muski_skinA; ?>);
            $('#skin_slika_<?php echo $_hsync_muski_skinA; ?>').attr("class","img-circle _hsync_reg_img_c");
        });

        $("#_hsync_bijelac_z").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","0");               
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinB; ?>.png");
            $('#_hsync_skin_dio_bijelac_z').show();
            $('#_hsync_skin_dio_crnac_z').hide();
            $('#_hsync_skin_dio_hispanac_z').hide();
            $('#_hsync_skin_dio_azijac_z').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_zenski_skinB; ?>);
            $('#skin_slika_<?php echo $_hsync_zenski_skinB; ?>').attr("class","img-circle _hsync_reg_img_c");   
        }); 

        $("#_hsync_crnac_z").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","1");               
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinC; ?>.png");
            $('#_hsync_skin_dio_bijelac_z').hide();
            $('#_hsync_skin_dio_crnac_z').show();
            $('#_hsync_skin_dio_hispanac_z').hide();
            $('#_hsync_skin_dio_azijac_z').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_zenski_skinC; ?>);
            $('#skin_slika_<?php echo $_hsync_zenski_skinC; ?>').attr("class","img-circle _hsync_reg_img_c");
        }); 

        $("#_hsync_hispanac_z").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","2");
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinH; ?>.png");
            $('#_hsync_skin_dio_bijelac_z').hide();
            $('#_hsync_skin_dio_crnac_z').hide();
            $('#_hsync_skin_dio_hispanac_z').show();
            $('#_hsync_skin_dio_azijac_z').hide();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_zenski_skinH; ?>);
            $('#skin_slika_<?php echo $_hsync_zenski_skinH; ?>').attr("class","img-circle _hsync_reg_img_c");
        }); 

        $("#_hsync_azijac_z").click(function() {
            $('#_hsync_trenutna_rasa').attr("value","3");               
            $('#_hsync_trenutni_skin').attr("src","_hsync_style/_hsync_images/_hsync_skins/_hsync_skin_<?php echo $_hsync_zenski_skinA; ?>.png");
            $('#_hsync_skin_dio_bijelac_z').hide();
            $('#_hsync_skin_dio_crnac_z').hide();
            $('#_hsync_skin_dio_hispanac_z').hide();
            $('#_hsync_skin_dio_azijac_z').show();
            $('#skin_slika_'+ $("#_hsync_trenutni_skin_id").val() +'').attr("class","img-circle _hsync_reg_img");       
            $('#_hsync_trenutni_skin_id').attr("value", <?php echo $_hsync_zenski_skinA; ?>);
            $('#skin_slika_<?php echo $_hsync_zenski_skinA; ?>').attr("class","img-circle _hsync_reg_img_c");
        });     

1 个答案:

答案 0 :(得分:0)

你的代码有些令人困惑,但从我看到你有一些名为属性name="_hsync_rasa"的单选按钮,所以你可以使用change事件检测这些元素的任何变化:

$( "input[name=_hsync_rasa]:radio" ).on('change', function() {
    var selectedRadio = $( "input[name=_hsync_rasa]:radio:checked" );
    alert( 'Selected ID - '  + selectedRadio.attr('id') );
});

这只是获取正确无线电元素的一个例子,但由于你的代码不够清晰(对我来说),这是我能帮到的最好的。

您可能希望将.on('change', function() {替换为.on('click', function() {,因为它可能会触发事件两次(一次用于未经检查的元素,紧接在新选择的无线电之后)。不确定,但万一你发现它正在发生。