【问题标题】:change dropdown value when checkbox is selected选中复选框时更改下拉值
【发布时间】:2014-02-02 17:36:58
【问题描述】:

我有这个表格,我想在选中复选框时保留下拉内容中的最高值:

<html>
<head><title>Check Box</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

</head>
<form>
<input type="checkbox" value="0" id="chk">Other
<input type="checkbox" value="1" id="chk1">Moderate incident
<input type="checkbox" value="2" id="chk2">Signifiant indcident
<input type="checkbox" value="3" id="chk3">Weather
<input type="checkbox" value="4" id="chk4">Severe
<input type="checkbox" value="2" id="chk5">Signifiant incident
<select id="test">
<option value="0">0 - Nothing important</option>
<option value="1">1 - Moderate incident</option>
<option value="2">2 - Signifiant incident</option>
<option value="3">3 - Weather Conditions</option>
<option value="4">4 - Severe incident</option>
</select>

</form>
<script>
$('input[type="checkbox"]').click(function(){

    if ($(this).is(':checked'))
        $("#test").val($(this).val());
});
</script>
</html>

当检查复选框时,从下拉下降更改时,如果复选框的值小于已经选中的值,我不想从下拉列表中更改值,但我希望最高值保持下拉列表。

例如:如果下拉列表中的最大值是 3,并且我检查值 2,我不想在下拉列表中更改它,只有当我检查值 4 时。

【问题讨论】:

    标签: jquery forms checkbox


    【解决方案1】:

    工作示例http://jsfiddle.net/xv33z/

    $('input[type="checkbox"]').click(function () {
    var x = $("#test").val();
        if ($(this).is(':checked')) {
            var y = $(this).val();
            if (x < y) {
                $("#test").val($(this).val());
            }
        }
    });
    

    首先,我们保存#test的当前值任何时间检查一个新的复选框。然后我们得到复选框的值。如果复选框的值大于选择框的值,那么我们更新选择框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2013-07-12
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多