.click事件不会触发

时间:2014-08-17 13:11:51

标签: javascript jquery

jQuery和Javascript相当新,但我似乎无法让这个工作......不管怎么说都不重复。

我想在一个页面上显示一些div id,当用户点击每个步骤中的按钮时,我会根据序列中的步骤编号显示和隐藏。

<div class="step" id="step1">
  <h3>1. Select size series</h3>
  <img id="size4" src="http://placehold.it/200x200&text=Size 4" />
  <img id="size6" src="http://placehold.it/200x200&text=Size 6" />
  <img id="size9" src="http://placehold.it/200x200&text=Size 9" />
  <p class="button">Next Step</p>
</div>

<div class="step" id="step2">
  <h3>2. Select Receptacle</h3>
  <img id="rivetOn" src="http://placehold.it/200x200&text=Rivet On" />
  <img id="weldOn" src="http://placehold.it/200x200&text=Weld On" />
  <img id="sideMount" src="http://placehold.it/200x200&text=Side Mount" />
  <img id="clipOn" src="http://placehold.it/200x200&text=Clip On" />
  <img id="miniClipOn" src="http://placehold.it/200x200&text=Mini Clip On" />
  <img id="frontMount" src="http://placehold.it/200x200&text=Front Mount" />
  <img id="pressIn" src="http://placehold.it/200x200&text=Press In" />
  <img id="clinch" src="http://placehold.it/200x200&text=Self Clinch" />
  <p class="button">Next Step</p>
</div>

当用户点击#step1中的一个图像时,会出现#step1 .button。

单击#step1 .button时,变量已设置,#step2显示。到目前为止,这是我的剧本,我想重复一遍。

$(document).ready(function() {
// Define global Variables.

var series;
var receptacle;
var tmtAdjust = 0;
var retainer;
var length;
var headStyle;
var finish;

var step = 1;

var stepStr = "step";
var stepId = stepStr.concat(step);
var stepButton = stepId.concat(" .button");

// Add border to clicked images 

$( '.step img' ).click(function() {

    $( '.step img' ).removeClass ( "selected" );
    $(this).addClass( "selected" );

}); // End of global .step img event

$( '#step1 img').click(function() {

    $( '#step1 .button').slideDown( "slow", function() {
        $(this).focus();
    });

    switch(this.id) {           
        case "size4":
        series = 4;         
        break;
        case "size6":
        series = 6;         
        break;
        case "size9":
        series = 9;         
        break;
    }

    $("#stud-series").text(series);

}); // End of Series step img click event

$( '#step2 img').click(function() {

    $( '#step2 .button').slideDown( "slow", function() {
        $(this).focus();
    });

    switch(this.id) {

        case "rivetOn":
        receptacle = "A";
        $("#receptacle-part-number").text("D8-33" + series + "-400-121");                       
        break;

        case "weldOn":
        receptacle = "A";
        $("#receptacle-part-number").text("D8-33" + series + "-500-121");
        break;

        case "sideMount":
        receptacle = "B";
        $("#receptacle-part-number").text("D8-33" + series + "-310-121");
        break;

        case "clipOn":
        receptacle = "B";
        $("#receptacle-part-number").text("D8-33" + series + "-300-121");
        break;

        case "miniClipOn":
        receptacle = "B";
        $("#receptacle-part-number").text("D8-33" + series + "-330-121");
        break;

        case "miniClipOnStainless":
        receptacle = "B";
        $("#receptacle-part-number").text("D8-33" + series + "-330-300");
        break;

        case "frontMount":
        receptacle = "C";
        $("#receptacle-part-number").text("D8-33" + series + "-200-190");
        break;

        case "pressIn":
        receptacle = "D";
        $("#receptacle-part-number").text("D8-33" + series + "-100-300");
        break;

        case "selfClinch":
        receptacle = "D";
        $("#receptacle-part-number").text("D8-33" + series + "-110-190");
        break;
    }

}); // End of Receptacle step img click event

// Show/hide next step on selction.

$("#" + stepButton).click(function() {

    alert("Button Clicked");
    $("#" + stepId).slideUp( "slow");
    switch(step) {

        case 1:

            switch(series) {
                case 4:
                $("#miniClipOn").hide();
                $("#miniClipOnStainless").hide();
                $("#cupWasher").hide();
                $("#ejectorSpring").hide();
                $("#tamperResist").hide();
                $("#stainless").hide();
                break;

                case 9:
                $("#miniClipOn").hide();
                $("#miniClipOnStainless").hide();
                $("#cupWasher").hide();
                $("#ejectorSpring").hide();
                $("#retainingSpringShort").hide();
                $("#tamperResist").hide();
                $("#stainless").hide();
                break;
            }

        default: 
        break;  

    }

    step++;
    stepId = stepStr.concat(step);
    stepButton = stepId.concat(" .button");


    $("#" + stepId).slideDown( "slow" );    


}); // End of "Next Step" button click event.

}); // End of Document.Ready

问题是这适用于#step1 ......但不适用于第2步。

链接 http://craigstruthers.com/quarter-turn-selector/d8.html

相关CSS

    .step img{
    border:1px solid #ffffff;
}

    .step img.selected, .step img:hover {
    border:1px solid #DA2328;
    }

.step .button {
    display:none;
}

#step2, #step3, #step4, #step5, #step6, #step7 {
    display:none;
}

1 个答案:

答案 0 :(得分:1)

如果要绑定多个步骤,只需使用类而不是id:

$('.step img').on('click', function() {
   var $step = $(this).closest('.step');
   alert($step.attr('id')  + ' was clicked');  
});

或者您也可以在文档就绪处理程序中迭代您的步骤:

$.each([1,2,3,4,5,6,7], function(i, stepId) {
  $('#step' + stepId).on('click', function() {
    alert(stepId + ' was clicked');
  });
});