简体   繁体   English

如果选中复选框,则更改选择选项

[英]if checkbox checked change the select options

I would like to generate selects options depends on checkbox. 我想生成选择选项取决于复选框。 Like I have checkbox with 3 values: Film, Videoclip, Serial and <select> form. 就像我有3个值的复选框一样:Film,Videoclip,Serial和<select>形式。 When I click "Film" I would like to have <select> with option: Comedy, Horror & when I "Videoclip" <select> will change values to: Hip-Hop, Pop. 当我单击“电影”时,我希望具有<select>选项:喜剧,恐怖和“ Videoclip”时, <select>会将值更改为:“嘻哈音乐”,“流行音乐”。

I'm trying for a few hours with javascript & jquery but still nothing :/ 我正在尝试用javascript&jquery几个小时,但还是没有什么:/

Can you try using jQuery and events something like this? 您可以尝试使用jQuery和类似事件的事件吗?

 $(function () { var checked = ["Check 1", "Check 2", "Check 3"]; var unchecked = ["Uncheck 1", "Uncheck 2", "Uncheck 3"]; function updateSelect (arr) { $("select").html(""); $.each(arr, function (i, v) { $("select").append('<option value="' + v + '">' + v + '</option>'); }); } updateSelect (unchecked); $("#check").change(function () { if (this.checked) updateSelect(checked); else updateSelect(unchecked); }); }); 
 <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <label><input type="checkbox" id="check" /> Check</label><br /> <select></select> 

Your snippet is now ready. 现在您的代码段已准备就绪。 Have a look: 看一看:

 $(function () { var iFilm = ["Comedy", "Horror", "Sci-Fi"]; var iVideoClip = ["Hip-Hop", "Pop", "Rap"]; var iSerial = ["Serial 1", "Serial 2", "Serial 3"]; $("input:checkbox").change(function () { $("select").html(""); $("input:checked").each(function () { addItemsFromArray(eval("i" + this.id)); }); }); function addItemsFromArray (arr) { $.each(arr, function (i, v) { $("select").append('<option value="' + v + '">' + v + '</option>'); }); } }); 
 <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <label><input type="checkbox" id="Film" /> Film</label><br /> <label><input type="checkbox" id="VideoClip" /> VideoClip</label><br /> <label><input type="checkbox" id="Serial" /> Serial</label><br /> <select></select> 

Note: I have used eval , which is strongly discouraged. 注意:强烈建议不要使用eval Without using eval , the other part is using data-* attributes. 不使用eval ,另一部分使用data-*属性。

 $(function () { var data = {}; data.iFilm = ["Comedy", "Horror", "Sci-Fi"]; data.iVideoClip = ["Hip-Hop", "Pop", "Rap"]; data.iSerial = ["Serial 1", "Serial 2", "Serial 3"]; $("input:checkbox").change(function () { $("select").html(""); $("input:checked").each(function () { addItemsFromArray(data[$(this).attr("data-content")]); }); }); function addItemsFromArray (arr) { $.each(arr, function (i, v) { $("select").append('<option value="' + v + '">' + v + '</option>'); }); } }); 
 <script src="https://code.jquery.com/jquery-1.9.1.js"></script> <label><input type="checkbox" id="Film" data-content="iFilm" /> Film</label><br /> <label><input type="checkbox" id="VideoClip" data-content="iVideoClip" /> VideoClip</label><br /> <label><input type="checkbox" id="Serial" data-content="iSerial" /> Serial</label><br /> <select></select> 

On a different note, if you wanna make only one option selectable at a time, feel free to change from type="checkbox" to type="radio" and give the same name. 另一方面,如果您想一次仅选择一个选项,请随时从type="checkbox"更改为type="radio"并使用相同的名称。

From your questions, I assume you have 3 checkbox with different value for each. 根据您的问题,我假设您有3个复选框,每个复选框都有不同的值。 If your application only allows 1 option, why dont use radio button instead? 如果您的应用程序仅允许1个选项,为什么不使用单选按钮呢?

Aside from that, it is actually quite easy. 除此之外,它实际上很容易。 Just put the "onchange" event listener on your checkbox and then populate your <select> tag (whether using pre-written variable or ajax) 只需将“ onchange”事件侦听器放在您的复选框上,然后填充您的<select>标记(无论使用预写变量还是ajax)

simple example in vanilla js 香草js中的简单示例

<input type="checkbox" value="film" id="film">
<select id="someDropdown"></select>

<script>
    var film = document.getElementById("film");
    film.addEventListener("change", function(){
        // populate the dropdown list
        var options = "<option>Comedy</option>" + "<option>Horror</option>"
        document.getElementById("someDropdown").innerHTML = options;
    });
</script>

Try this: 尝试这个:

var filmValues = {
    comedy: 'Comedy',
    horror: 'Horror'
}

$('#filmCb').change(function () {
    if ($(this).is(":checked")) {
        $.each(filmValues, function (key, value) {
            $('#filmSelect')
                .append($("<option></option>")
                .attr("value", key)
                .text(value));
        });
    } else {
        $('#filmSelect').empty()
    }
});

The html: 的HTML:

<form action="">
    <input id="filmCb" type="checkbox" name="param" value="film">Film
    <select id="filmSelect"></select>
    <br>
</form>
<?php // Genre 

$query  = "SELECT genre_pl FROM genre WHERE is_movie = 1 ";
$result = mysqli_query($connection, $query);
// Test if there was a query error
confirm_query($result); ?>
<script> var options_film = ""; </script>
    <?php
        while($row = mysqli_fetch_row($result)){
      ?>
    <pre> <?php var_dump($row) ?> </pre>
    <script> options_film += "<option>" + <?php echo json_encode($row); ?> +     "</option>"; </script>
           <?php } ?>

<?php mysqli_free_result($result); ?>
<script type="text/javascript">
var film = document.getElementById("film");
    film.addEventListener("change", function(){
    // populate the dropdown list
    document.getElementById("someDropdown").innerHTML = options_film;
});

It works! 有用! But I feel it's sloppy programming. 但是我觉得这是草率的编程。 It is okay to mix javascript with php just like that? 可以像这样将javascript与php混合在一起吗?

here is a small bit of code to make each checkbox selected individually. 这是一小段代码,用于分别选中每个复选框。

$(document).ready(function(){
$("#Film").click(function(){
   `$("#Film").prop("checked", false);
});
$("#VideoClip").click(function(){
    `$("#VideoClip").prop("checked", false);
});
$("#Serial").click(function(){
    `$("#Serial").prop("checked", false);
});
});

And the HTML : 和HTML:

 <label><input type="checkbox" id="Film" data-content="iFilm" /> Film</label><br />
 <label><input type="checkbox" id="VideoClip" data-content="iVideoClip" /> 
 VideoClip</label><br />
 <label><input type="checkbox" id="Serial" data-content="iSerial" /> Serial</label> 
 <br 
 />

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

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