基于单选按钮选择的jQuery

基于单选按钮选择的jQuery

本文介绍了基于单选按钮选择的jQuery DatePicker更新选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,只要选择不同的单选按钮来获得一个jquery日期选取器的更新.

I am trying to get a jquery date picker to update whenever a different radio button is chosen.

<div style="padding-bottom: 50px;">
    <label>Location: </label>
    <input type="radio" name="locate" value="Internal">Internal
    <input type="radio" name="locate" value="External">External
</div>
<div>
    <label>Due Date: </label>
    <input type="text" name="dueDate" id="dueDate" size="25" placeholder="Please Enter A Due Date" autocomplete="off" readonly="true">
</div>

jQuery可以使用,但是每次单击新的单选按钮时,它不会更新datePicker.

The jQuery works but it doesn't update the datePicker every time a new radio button is clicked.

var locate = null;
$("input[name='locate']").click(function() {
    locate = this.value;

    if (locate == "Internal") {
        $( "#dueDate" ).datepicker( { minDate: '-6M', maxDate: '+6M' });
        alert("Internal");
    } else {
        $( "#dueDate" ).datepicker( { minDate: -0, maxDate: '+6M' });
        alert("External");
    }
});

推荐答案

您没有正确获取单选按钮的选中值.尝试更改您的if条件,如下所示.

You are not retrieving the radio button's checked value correctly. Try changing you if condition like below.

 if ($("input[name='locate']:checked").val() == 'Internal'){
    $( "#dueDate" ).datepicker( { minDate: '-6M', maxDate: '+6M' });
 }
 else {
    $( "#dueDate" ).datepicker( { minDate: -0, maxDate: '+6M' });
 }

更新1:使用destroy销毁日期选择器,并在单选按钮的选项更改时重新创建它.这是完整的代码.链接到有效的 DEMO

UPDATE 1: Use destroy to destroy the datepicker and recreate it as the radio button's option changes. Here is the complete code. Link to working DEMO

 $("input[name='locate']").click(function() {
    locate = this.value;
    var dateField = $('#dueDate');

 if ($("input[name='locate']:checked").val() == 'Internal'){
     dateField.datepicker('destroy');
     dateField.datepicker( { minDate: '-6M', maxDate: '+6M' });
 }
 else {
     dateField.datepicker('destroy');
     dateField.datepicker( { minDate: -0, maxDate: '+6M' });
 }

});

选择Internal后,您最多可以追溯到6个月;选择Extrenal时,则从当前日期开始.

When you choose Internal you'll be able to go back upto 6 months and when Extrenal is chosen then it's from the current date.

这篇关于基于单选按钮选择的jQuery DatePicker更新选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:24