选择jquery选择的下拉选项

时间:2013-08-09 13:57:04

标签: php jquery

我需要在通过ajax加载某些内容时选择选项。这是代码..

<select id="select" name="select" onchange="onClick(this.options[this.selectedIndex].value)">
    <option value="1"  >Yes</option>
    <option value="0">No</option>
function onClick(value) {
    var p = document.getElementById('select'); 
    if(p.value != value) {
            $('select>option:eq(1)').prop('selected',true)); /*this portion doesn't work */  
    } 
    setTimeout("onClick(1)",5000); /* value 1 for display yes which will display by ajax load file add.php but i can't select yes which already set by ajx url and load content */ 
    } 
    $.ajax({
        url:'add.php',
        type:'GET', 
        data : {id : value}, 
        success : function(response){
            $('div#content').html(response);
        } 
    });  
}});}

1 个答案:

答案 0 :(得分:0)

首先,您可以像这样更新代码: HTML:

<select id="select" name="select">
    <option value="1">Yes</option>
    <option value="0">No</option>
</select>

JS:

$(document).ready(function(){
  $('#select').on('change', function(){
     //Here $(this) is the "select" select;
     if($(this).val() !== '1'){
        $(this).val('1');   //You can set val to select the option which you want.
     }
     .....
  });
});

顺便说一句,我仍然无法遵循代码的其余部分。希望这对你有意义。