简体   繁体   English

根据复选框更改选择选项的值

[英]Change value of select option based on checkbox

Is there is way to change the value of select option if user check the check box. 如果用户选中复选框,是否有更改选项的值的方法。 If user select "four" from select option, its default value is "4".Now if user check the checkbox, select option value becomes "44". 如果用户从选择选项中选择“四”,则其默认值为“4”。如果用户选中该复选框,则选择选项值变为“44”。

<select name="sel" id="MySelect">
<option value="">Select One</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>

<input type="checkbox" name="btn_radio" />Discount

Just modify the <option> when the checkbox changes: 只需在复选框更改时修改<option>

 (function() { var discount = document.getElementById("discount"); var option = document.querySelector("#amount option[value='4']"); discount.addEventListener("change", function(e) { if (this.checked) { option.value = 44; option.textContent = "Fourty-Four"; } else { option.value = 4; option.textContent = "Four"; } console.log("value: %s, text: %s", option.value, option.textContent); }); })(); 
 <select id="amount"> <option value="">Select One</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> <option value="5">Five</option> <option value="6">Six</option> </select> <label><input type="checkbox" id="discount" />Discount</label> 

See also: document. getElementById() 另见: document. getElementById() document. getElementById() , document. querySelector() document. getElementById()document. querySelector() document. querySelector() , and EventTarget. addEventListener() document. querySelector()EventTarget. addEventListener() EventTarget. addEventListener()

if I understand you correctly, and if you can use jQuery it should be possible. 如果我理解正确,如果你可以使用jQuery它应该是可能的。

Since you need to have option with value "44" you need to add/remove this option dynamically, when user check/uncheck checkbox. 既然你需要有值“44”,你需要添加选项 /动态删除此选项,当用户选中/清除复选框。

I prepared simple example, it should be helpful for you: 我准备了简单的例子,它应该对你有所帮助:

https://jsfiddle.net/qv9nxhov/ https://jsfiddle.net/qv9nxhov/

$('select').on('change', function() {
    //show current value
    $('.result').text($(this).val());

    //remove discount when choose other option
    $('select option[value="44"]').remove();
    $('input').attr('checked', false);
});

$('input').on('click', function() {
    //check if checkox is checked    
    if ($(this).is(':checked')) {
        //add extra option
        $('select').append('<option value="44" selected="selected">Discount</option>');
    } else {
        //remove extra option
        $('select option[value="44"]').remove();
    }
    //show current value
    $('.result').text($('select').val());
});

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

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