复选框和下拉菜单之间的事件绑定
Event binding between checkbox and dropdown menu
这是我的表格:
<table>
<tr>
<td>
<label for="check">
<input type="checkbox" name="check" id="check" value="true" />
Select something
</label>
</td>
<td>
<label for="select">
<select name="select" id="select">
<option value="Default"></option>
<option value="A">A</option>
<option value="B">B</option>
</label>
<td>
</tr>
我试图将复选框功能与下拉菜单联系起来:如果复选框未选中,并且用户选择的选项值不是"Default"的选项,那么复选框应该自动选中并使用"selected"类突出显示。然后,当"Default"值被选中时,复选框应该被选中并且不突出显示,而不管它之前的状态如何。
jQuery("#check").on("click", function() {
if(!jQuery(this).is(":checked")) {
jQuery("#select").val("Default");
}
});
jQuery("#select").on("change", function() {
if(jQuery(this).val() === "Default") {
jQuery("#check").attr("checked", false);
}
});
尝试使用" onenge ",像这样:
<html>
<body>
<table>
<tr>
<td>
<label for="check">
<input type="checkbox" name="check" id="check" value="true" />
Select something
</label>
</td>
<td>
<label for="select">
<select name="select" id="select" onchange="Select_Change(this)">
<option value="Default"></option>
<option value="A">A</option>
<option value="B">B</option>
</label>
<td>
</tr>
<script>
function Select_Change( elem )
{
if (elem.value != 'Default')
{
CheckBox_Select( true );
}
else
{
CheckBox_Select( false );
}
}
function CheckBox_Select( check )
{
document.getElementById('check').checked = check;
}
</script>
</body>
</html>
第一个函数:
function selectValueChanged(){
var selected = document.getElementById("select").selectedIndex;
var options = document.getElementById("select").options;
if(document.getElementById("check").checked){
if(options[i].value == "Default"{
document.getElementById("check").checked = false;
document.getElementById("check").className = document.getElementById("check").className.replace(" selected", "");
}
else{
if(options[i].value != "Default"){
document.getElementById("check").checked = true;
document.getElementById("check").className += " selected";
}
}
}
然后给select一个onchage事件,使它看起来像:
<select name="select" id="select" onchange="selectValueChanged()">
相关文章:
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- 如何在tinymce编辑器中将点击事件绑定到html标签
- 如何将事件绑定到从AJAX请求注入的HTML
- 将click事件绑定到AngularJS指令中的子元素
- Jquery将事件绑定到页面上的多个实例
- 事件绑定到动态创建的元素
- 使用jQuery进行事件绑定
- 如何将单击事件绑定到其他类
- 更改事件绑定's函数在页面加载时调用
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 动态创建的DOM元素上的jQuery事件绑定
- 我是否可以使用 Angular 将鼠标向上事件绑定到文档正文
- 将事件绑定到除 data-rel= BACK 之外的所有锚标记
- 将函数事件绑定到更改函数的复选框/标签
- 使用 Angular 和 UI-bootstrap 对模态事件绑定无法工作的指令
- 聚合物 - 将事件绑定到动态创建的元素
- 将事件绑定到其他UI组件's函数,在Kendo MVVM中
- jQuery Mobile——事件绑定
- JQuery无法将模糊事件绑定到所有<输入>页面中的元素
- 将点击事件绑定到新元素&使得ID'是独一无二的