0

我需要在通过 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);
        } 
    });  
}});}
4

1 回答 1

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

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

于 2013-08-09T14:28:28.430 回答