jQuery根据锚点的点击改变选择框的值
jQuery change the select box value based on anchor clicked
当有人点击链接时,我需要改变选择框的值
<a class="cli" href="0">Jan</a>
<a class="cli" href="1">Feb</a>
<a class="cli" href="2">Mar</a>
<a class="cli" href="3">Apr</a>
<a class="cli" href="4">May</a>
<a class="cli" href="5">Jun</a>
[...]
<a class="cli" href="11">Dec</a>
<select class="ui-datepicker-month">
<option value="0" style="display: none;">Jan</option>
<option value="1" style="display: none;">Feb</option>
[...]
<option value="9" style="display: none;">Oct</option>
<option selected="selected" value="10" style="display: none;">Nov</option>
<option value="11" style="display: none;">Dec</option>
</select>
这是我到目前为止所做的,我以为它会很简单,只是我无法理解它:
$("a.cli").click(function(event){ //when anchor is clicked
event.preventDefault();
$(".ui-datepicker-month").val($(this).text());
});
});
我也试过了
$("a.cli").click(function(event){ //when anchor is clicked
event.preventDefault();
$(".ui-datepicker-month").val($(this).attr("href"));
});
});
还是不行
由于value
值保存在href
属性中,因此使用:
$("a.cli").click(function(event){ //when anchor is clicked
event.preventDefault();
$(".ui-datepicker-month").val($(this).attr("href"));
});
为什么更改值而不选择具有"clicked"值的选项?
<a class="cli" href="0">Jan</a>
<a class="cli" href="1">Feb</a>
<a class="cli" href="2">Mar</a>
<select id="month" name="month" class="ui-datepicker-month">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
</select>
脚本:
jQuery('a.cli').click(function(event) {
event.preventDefault();
jQuery('#month option').attr('selected', false);
jQuery('#month option[value="' + jQuery(this).attr('href') + '"]').attr('selected', true);
});
查看http://jsfiddle.net/jchiotaka/uaLXU/
试试这个:
...
<a onClick="$('select.ui-datepicker-month').val('1');">Feb</a>
...
<select class="ui-datepicker-month">
...
<option value="1">Feb</option>
...
</select>
相关文章:
- 如何改变“;尺寸“;单击选择时的选择
- 用于根据用户改变文本区域的字体颜色的JavaScript'的选择
- WooCommerce:逐步改变产品选择
- .change(function() 每当<选择>有输入时就会改变
- 触发“;改变“;带有多个选择器的jQuery
- 当下拉选择通过模型改变时,如何触发jquery事件
- 根据所述选择框的值来改变所述图像大小
- 改变“;选择“;下拉列表的属性无法正常工作
- 改变<选择>有状态React组件中的值
- 如果选择字段发生更改,Jquery将更改变量值
- 当我改变选择标记为什么不在输入id=“0”中显示数据时;消息”;
- 聚合物通过选择器改变风格
- jquery改变了选择上的选项,但Chrome却出现了奇怪的结果
- ChartJS根据下拉选择改变图形
- 选择改变颜色图案不会显示
- HTML5颜色选择改变跨度颜色
- 当根据下拉选择改变图像时,为图像添加超链接
- Angularjs:当单选按钮选择改变时更新模型
- 选择改变背景色
- 通过组合框选择改变href值