简体   繁体   English

在一个函数中使用多个if语句?

[英]Using multiple if statements in one function?

I want to add text to the URL whenever a certain checkbox is checked. 每当选中某个复选框时,我都想向URL添加文本。 I thought im just gonna add multiple if statements for every checkbox, however that did not work. 我认为我只是要为每个复选框添加多个if语句,但是那没有用。

$("#button1").on("click", function(){
    url = "";
    if (auswahl_index1[0].checked) {
        url = page + option_0;
    }
    if (auswahl_index2[0].checked) {
        url = page + option_1;
    }
    window.location.href = url; 
});

 var auswahl_index1 = document.getElementById('yes1'); var auswahl_index2 = document.getElementById('no1'); var auswahl_index1 = document.getElementById('yes2'); var auswahl_index2 = document.getElementById('no2'); var page = "http://stackoverflow.com"; var url = page; $("#button1").on("click", function(){ url = ""; if (auswahl_index1[0].checked) { url = page + "test1"; } if (auswahl_index2[0].checked) { url = page + "test2"; } if (auswahl_index3[0].checked) { url = page + "test3"; } if (auswahl_index4[0].checked) { url = page + "test4"; } window.location.href = url; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" name="rdo1" id="yes1"/> <input type="radio" name="rdo1" id="no1"/> <input type="radio" name="rdo2" id="yes2"/> <input type="radio" name="rdo2" id="no2"/> <button type="button" id="button1">Button</button> 

I tryed to research what else to do and all I found is using "else if" for the second statement. 我试图研究其他事情,发现第二条语句只使用“ else if”。 Hovever that wouldnt work neither since that would mean that it would only activate if the first statement would be false, but both at the same time should be able to add to the url. 但是,这将不会起作用,因为那将意味着仅在第一个语句为false时才会激活,但两者都应能够同时添加到url中。

Sorry if this is a stupid question, im just really new to jquery and I couldnt find anything by googleing my problems. 抱歉,如果这是一个愚蠢的问题,那对jquery来说真的很新,我无法通过谷歌搜索找到任何问题。 If you need more information about my problem, just ask please :) 如果您需要有关我的问题的更多信息,请问:)

I know in the past I came across issues with using .checked on checkboxes. 我知道过去我在复选框上使用.checked遇到过问题。

.prop seems to solve a lot of issues. .prop似乎解决了很多问题。

not 100% if its .prop('checked', 'checked') or .prop('checked') try either. 如果其.prop('checked','checked')或.prop('checked')尝试两者之一,则不是100%。

Also a note to mind: If they are checkboxes that means multiple of them can be checked. 还要注意:如果它们是复选框,则意味着可以选中其中的多个。 Use radio buttons instead. 请改用单选按钮。

var auswahl_index1 = document.getElementsByClassName('yes1');
var auswahl_index2 = document.getElementsByClassName('no1');


$("#button1").on("click", function(){
    url = "";
    if (auswahl_index1.prop('checked')) {
        url = page + option_0;
    }else if(auswahl_index2.prop('checked')) {
        url = page + option_1;
    } else {
       console.log('say something');
       return null;
    }
    window.location.href = url; 
});

Alternative function you could do is, add same class to each checkbox: 您可以执行的替代功能是,将相同的类添加到每个复选框:

$('.class-added').change(function(){
    url = "";
    if (auswahl_index1.prop('checked')) {
        url = page + option_0;
    }
    else if (auswahl_index2.prop('checked') {
        url = page + option_1;
    }
    window.location.href = url;
});

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

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