weixin_33709590 2017-08-01 06:20 采纳率: 0%
浏览 26

选择带有值的选项

How I can select option with value after ajax success? I do like this, but it doesn't work.

$.ajax({
                        type: "POST",
                        url: "/ax.php",
                        cache: false,
                        data: { action: 'show', id: pid},
                        success: function(responce){ 
                            $('div[name="qwerty"]').html(responce); 
var some_value = <?=$ads['cat2']?>;

$('#cat_2 select option[value="'+some_value +'"]').prop('selected', true);
 ...........................................................

P.S There is no error in console. And some_value = 10, and there is option with value 10 in select, but not selected. P.P.S I use async:false, it doesn't work either.

  • 写回答

1条回答 默认 最新

  • weixin_33738578 2017-08-01 06:28
    关注

    try setting the attribute of option element. do not forget to reset this attribute on all options before selecting one

    $.ajax({
                        type: "POST",
                        url: "/ax.php",
                        cache: false,
                        data: { action: 'show', id: pid},
                        success: function(responce){ 
                            $('div[name="qwerty"]').html(responce); 
     var some_value = <?=$ads['cat2']?>;
    
     $('#cat_2 select').val(some_value);
    

    $(document).ready(function() {
      $("button").click(function() {
        var val = $("select").val();
        if(val == 3) val = 1;
        else val++;
        
        $("select").val(val);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button>Change value</button>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了