jQuery change the select box value based on anchor clicked

后端 未结 3 1174
轮回少年
轮回少年 2021-01-21 16:09

When some one clicks a link I need the select box value to change

Jan
Feb         


        
相关标签:
3条回答
  • 2021-01-21 16:40

    Since the value values are saved at the href attribute, use:

    $("a.cli").click(function(event){ //when anchor is clicked
        event.preventDefault();
        $(".ui-datepicker-month").val($(this).attr("href"));
    });  
    
    0 讨论(0)
  • 2021-01-21 16:44

    Try this:

    ...
    <a onClick="$('select.ui-datepicker-month').val('1');">Feb</a>
    ...
    
    
    <select class="ui-datepicker-month">
    ...
    <option value="1">Feb</option>
    ...
    </select>
    
    0 讨论(0)
  • 2021-01-21 16:50

    Why change the value and not select the option that has the "clicked" value?

    <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>
    

    Script:

    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);
    });
    

    Check it out http://jsfiddle.net/jchiotaka/uaLXU/

    0 讨论(0)
提交回复
热议问题