简体   繁体   English

Javascript下一个按钮数组

[英]Javascript next button array

I am unrehearsed in javascript and was hoping for some help with a next button that links to an id based on the array.我对 javascript 没有预习,并希望对链接到基于数组的 id 的下一个按钮有所帮助。 Here is the array这是数组

var baseline_next=new Array();
baseline_next[0]="#one";      
baseline_next[1]="#two";
baseline_next[2]="#three";
baseline_next[3]="#four";
baseline_next[4]="#five";
baseline_next[5]="#six";
baseline_next[6]="#six2";
baseline_next[7]="#seven";
baseline_next[8]="#eight";
baseline_next[9]="#nine";
baseline_next[10]="#ten";
baseline_next[11]="#eleven";
baseline_next[13]="#thirteen";
baseline_next[14]="#fourteen";
baseline_next[15]="#fifteen";
baseline_next[16]="#sixteen";
baseline_next[17]="#seventeen";
baseline_next[18]="#eighteen";
baseline_next[19]="#nineteen";
baseline_next[20]="#twenty";
baseline_next[21]="#twentyone";
baseline_next[22]="#twentytwo";
baseline_next[22]="#twentythree";

Basically what I need is, when the next button is clicked first ("0" ~ #one) I need the next buttons id to become two, and when clicked again it needs to become #three.基本上我需要的是,当首先单击下一个按钮时(“0”~#one)我需要下一个按钮 id 变为两个,再次单击时它需要变为 #three。 I have no idea how to link an array to a button.我不知道如何将数组链接到按钮。 The reason I need this to happen is because I am using ajax to .load div contents, so the next button doesn't actually submit, it just becomes a new next button.我需要这种情况发生的原因是因为我使用 ajax 来加载 div 内容,所以下一个按钮实际上并没有提交,它只是成为一个新的下一个按钮。 I dont know if it matters, but here is a part of what the button would cause to happen on click.我不知道这是否重要,但这是按钮在点击时会导致发生的部分内容。

$('#one').click(function(){
        $("#area").hide("slide", { direction: "left" }, 500);
        $("#area").load("test_it.jsp #area");
        $("#area").show("slide", { direction: "right" }, 500);
        $("#two").show();

        });
$('#two').click(function(){
    if((document.form1.baseline_01[0].checked || document.form1.baseline_01[1].checked| document.form1.baseline_01[2].checked)
    && (document.form1.baseline_02[0].checked || document.form1.baseline_02[1].checked) 
    && (document.form1.baseline_03_native.checked || document.form1.baseline_03_asian.checked|| document.form1.baseline_03_black.checked|| document.form1.baseline_03_pacific.checked|| document.form1.baseline_03_white.checked|| document.form1.baseline_03_other.checked)){
        if(document.form1.baseline_03_other.checked && document.form1.baseline_03_other_text.value==""){
            alert("Please fill in what other race you concider yourself to be.");
            return false;
        }else{
        $("#area").hide("slide", { direction: "left" }, 500);
        $("#area").load("test_it.jsp #area2");
        $("#area").show("slide", { direction: "right" }, 500);
            $("#three").show();
            return true;
        }
    }else{
        alert("Please select an answer for each question.");
        return false;
    }
        });

I must apologize in advanced if my question is hard to follow, I just started coding this year.如果我的问题难以理解,我必须提前道歉,我今年才开始编码。

I think that you should consider reworking your code a bit to not have this list of IDs and use a single class on all of the elements.我认为您应该考虑稍微修改一下您的代码,以便没有此 ID 列表并在所有元素上使用单个类。 It would help to see how your HTML is set up to get the next element based on class as well.这将有助于了解如何设置 HTML 以获取基于类的下一个元素。

Since you already have this array of doom, though, I guess we'll have to use it.不过,既然你已经有了这一系列的厄运,我想我们将不得不使用它。 Ideally, you have the same class on each element.理想情况下,每个元素都有相同的类。 Let's say, .number .比方说, .number

$(".number").on('click', function () {
    var nextID = baseline_next.indexOf(this.id) + 1;
});

If you can't use .number , then you can do the same thing except with a list of every ID that needs to be bound .. ouch.如果你不能使用.number ,那么你可以做同样的事情,除了需要绑定的每个 ID 的列表......哎哟。 In the above function, nextID should be the index of the ID that comes after the clicked element.在上面的函数中, nextID应该是被点击元素之后的 ID 的索引。

By the way, IDs can be numbers, and you could even use data-id that contains the number.顺便说一下,ID 可以是数字,您甚至可以使用包含数字的data-id That would be easier too.那也会容易些。

OK I really need to ask why are you doing this, are you sure that creating more than 20 ID's called like that is the better way to do want you want?好的,我真的需要问你为什么要这样做,你确定创建 20 多个这样的 ID 是实现你想要的更好的方法吗?

Anyways, first of all, you can create the array in a shorthand way:无论如何,首先,您可以以速记方式创建数组:

var baseline_next= ["#one", "#two", "#four", "#five", "#six", "#six2" ..., "#twentythree";

(btw "#six2"? really?) (顺便说一句“#six2”?真的吗?)

then do something like this:然后做这样的事情:

var counter = 0; // start for the first one
$("#button").on('click', function () {
    var id = counter++;
    baseline[id]; // and I don't know what you want to do with this, but here it is!
});

Ancient question.古老的问题。 (7 years wowzers). (7 年哇塞)。 Here is some logic that I used for determining the next item in an array using indexOf .这是我用于使用indexOf确定数组中的下一项的一些逻辑。 This will also start from the beginning once you reach the end of your array.一旦到达数组的末尾,这也将从头开始。

        let options = [true,false,null];
        let current = options.indexOf(this.get('order'));
        let next = (current+1 > options.length-1 ? 0 : current+1);
        this.set('order', options[next]);

It can be simplified as follows:可以简化如下:

let baseline_next = ["#one", "#two", "#three"];

Don't use a var as it behaves unpredictably.不要使用var因为它的行为不可预测。

Add a variable count to change the index of the array.添加变量count以更改数组的索引。

let count = 0;
let baseline_next = ["#one", "#two", "#three"];
$(baseline_next[count]).click(function() {
    count++;
});

Or要么

let count = 0;
let baseline_next = ["#one", "#two", "#three"];
$("#one").click(function() {
    $(baseline_next[count]).click();
    count++;
});

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

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