简体   繁体   English

如何通过选择复选框和下拉列表来使用Javascript更改标签值

[英]How to Javascript change Label value from selecting Checkbox and drop-down list

I have 2 checkboxes and one of it contains a drop-down list with 5 elements. 我有2个复选框,其中一个包含5个元素的下拉列表。

  • Checkbox A 复选框A
  • Checkbox B: drop-down list 复选框B:下拉列表
  • Textbox 文本框

First have to check if the checbox B is checked, then Textbox only can be showing value, otherwise is empty. 首先必须检查checbox B是否被选中,然后Textbox仅可以显示值,否则为空。 Secondly, if checkbox B checked and a value from drop-down list been selected, then the Textbox must show value immidiately on the changes. 其次,如果选中了复选框B,并且从下拉列表中选择了一个值,则文本框必须立即在更改上显示值。

<input type="checkbox" name="events" value="event02 />
Event 01
</input>
<br/>
<input type="checkbox" name="events" value="event02 />
Event 02:
<select name="selectOne" onchange="amount()">
    <option value="00" selected="selected">00</option>
    <option value="01">01</option>
    <option value="02">02</option>
    <option value="03">03</option>
    <option value="04">04</option>
    <option value="05">05</option>
    <option value="06">06</option>
    <option value="07">07</option>
</select>
<br/>
<input type="text" name="displayTxtBox" id="displayTxtBox" size="10" maxlength="50" value="" />

I have no idea in Javascript how to write code detect the Checkbox B detected and Drop-down List selected only display value on Textbox. 我不知道在Javascript中如何编写代码来检测到检测到的复选框B,并且下拉列表选择了仅在文本框中显示值。

I think it needs onChange and some function in Javascript. 我认为它需要onChange和Javascript中的某些功能。

Please kindly guide me. 请指导我。

Given corrected HTML, with added id attributes and label elements to provide additional click-targets for the various form elements: 给定已更正的HTML,并添加了id属性和label元素,以为各种表单元素提供其他点击目标:

<input type="checkbox" id="input1" name="events" value="event01" />
<label for="input1">Event 01</label>
<br/>
<input type="checkbox" id="input2" name="events" value="event02" />
<label for="input2">Event 02</label>

<select name="selectOne">
    <option value="00" selected="selected">00</option>
    <option value="01">01</option>
    <option value="02">02</option>
    <option value="03">03</option>
    <option value="04">04</option>
    <option value="05">05</option>
    <option value="06">06</option>
    <option value="07">07</option>
</select>
<br/>
<input type="text" name="displayTxtBox" id="displayTxtBox" size="10" maxlength="50" value="" />​

The following JavaScript seems to work for your intended results, though I wasn't entirely certain of your intent: 以下JavaScript似乎可以达到您预期的效果,尽管我不确定您的意图是什么:

function showAmount(el, selectEl, targetEl, clearOnUncheck) {
    /* the following is an optional, Boolean, variable, so a ternary
       conditional is used, which checks that 'clearOnUncheck' exists, 
       if it does then the variable is assigned its own value,
       if *not* then it's explicitly assigned the value of false
       note it's Boolean, *not* a string: don't quote this value */
    var clearOnUncheck = clearOnUncheck ? clearOnUncheck : false;
    if (!el || !selectEl || !targetEl) {
        // sanity checking, these are *required* arguments for the function
        return false;
    }
    // if the checkbox is checked, continue
    else if (el.checked) {
        /* assigning the conditions I'm testing to variables, so if conditions
           change they only have to be changed in one place */
        var index = selectEl.selectedIndex,
            textcontent = (typeof(el.textContent) != 'undefined');
        if (textcontent) {
            // up-to-date browsers
               /* sets the value of the text-input to be the string contained
                  within the selected option from the select element */
            targetEl.value = selectEl
                .getElementsByTagName('option')[index]
                .textContent;
        }
        else if (window.innerText) {
            // IE < 8
               // as above, but uses innerText for IE
            targetEl.value = selectEl
                .getElementsByTagName('option')[index]
                .innerText;
        }
    }
    /* if the checkbox is unchecked, and you've set the Boolean for the
       optional clearOnUncheck to true (remember, *not* a string, don't quote) */
    else if (!el.checked && clearOnUncheck) {
        /* if clearOnUncheck is true, the value is cleared from the text-input,
           if clearOnUncheck is set to false, or not-set, the text-box value
           persists after unchecking the checkbox */
        targetEl.value = '';
    }
}

// references to the elements
var input2 = document.getElementById('input2'),
    select = document.getElementsByName('selectOne')[0],
    textInput = document.getElementById('displayTxtBox');

// binding the function to the onchange event of the input2 and selectOne elements.
input2.onchange = function() {
    showAmount(input2, select, textInput, true);
};
select.onchange = function() {
    /* because the clearOnUncheck argument depends on the changing of the
       checkbox there's no point in passing it to the showAmount() function
       in the select's onchange event-handler */
    showAmount(input2, select, textInput);
};​

JS Fiddle demo with clearOnUncheck == true 带有clearOnUncheck == true JS小提琴演示

JS Fiddle demo with clearOnUncheck == false 带有clearOnUncheck == false JS小提琴演示

JS Fiddle demo with clearOnUncheck not set 未设置带有clearOnUncheck JS Fiddle演示

References: 参考文献:

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

相关问题 如何使用简单的javascript从下拉列表中动态更改值? - How to dynamically change value from drop-down list with simple javascript? 从下拉列表中选择数据库生成的值? - Selecting database generated value from a drop-down list? 如何更改下拉列表中的选定值 - How change the selected value in drop-down list 使用Javascript获取下拉列表的值 - Getting Value of Drop-Down List with Javascript 如何使用JavaScript / JQuery获取下拉列表的选定值 - How to get the selected value of a drop-down list with JavaScript/JQuery 使用JQuery通过更改下拉值来更改标签的值 - Change the value of label with change in drop-down value using JQuery 如何在选择下拉列表中的复选框后清除v-autocomplete(多个)搜索输入? - How to clear v-autocomplete (multiple) search input after selecting the checkbox in the drop-down list? JavaScript - 如何从下拉菜单中更改项目 - JavaScript - how to change item from drop-down menu 如何根据 JavaScript 下拉列表中的选择更改值? - How to change values based on the selection from the drop-down list in JavaScript? 如何根据下拉菜单选择从下拉菜单中选择后显示复选框列表? - How to make a checkbox list appear after a selection from a drop-down menu, depending on drop-down menu selection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM