Javascript 如何通过选中复选框和下拉列表更改标签值
我有2个复选框,其中一个包含5个元素的下拉列表 复选框A 复选框B:下拉列表 文本框 首先要检查checbox B是否被选中,然后文本框只能显示值,否则为空。 其次,如果选中复选框B并从下拉列表中选择值,则文本框必须立即在更改中显示值Javascript 如何通过选中复选框和下拉列表更改标签值,javascript,drop-down-menu,checkbox,textbox,onchange,Javascript,Drop Down Menu,Checkbox,Textbox,Onchange,我有2个复选框,其中一个包含5个元素的下拉列表 复选框A 复选框B:下拉列表 文本框 首先要检查checbox B是否被选中,然后文本框只能显示值,否则为空。 其次,如果选中复选框B并从下拉列表中选择值,则文本框必须立即在更改中显示值 <input type="checkbox" name="events" value="event02 /> Event 01 </input> <br/> <input type="checkbox" name="eve
<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="" />
在Javascript中,我不知道如何编写代码来检测复选框B detected和下拉列表selected only display value on Textbox
我认为它需要onChange和一些Javascript函数
请指导我。给定更正的HTML,添加id属性和标签元素,为各种表单元素提供额外的点击目标:
<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="" />
下面的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);
};
参考资料:
.
.
.
.
.
.
.
.
输入元素不能包含其他元素,因此它们没有结束标记:因此您的第一个输入是无效的HTML,这意味着浏览器尝试自动错误恢复,结果不可预测。请打开该文本,删除结束标记,并使用自动结束标记,而不是使用其他输入元素。另外,第二个输入标记的value属性有一个未关闭的字符串。