【问题标题】:Set exact option for select-box which generated on the fly with AJAX为使用 AJAX 即时生成的选择框设置精确选项
【发布时间】:2011-12-01 19:54:25
【问题描述】:

我的 AJAX 函数会即时生成 #parent 选择框。问题是,我无法在菜单生成后设置确切的选项

试过了

window.onload = function () {
var parent='1';
$('#parent').val(parent);   
}   

$(document).ready(function () {
var parent='1';
$('#parent').val(parent);   
});

没有成功!有什么建议吗?

【问题讨论】:

    标签: javascript ajax jquery select


    【解决方案1】:

    onloadready 都不会等待 Ajax 请求。将函数移动到您传递给成功处理程序的回调。

    【讨论】:

      【解决方案2】:

      基本上...您需要确保在<option> 中有一个值。 这是示例

      (HTML - script.html)

      <select id="parent">
          <option value="0">First</option>
          <option value="1">Second</option>
      </select>
      

      (JS - index.html)

      $(function(){
          $.get('script.html', function(data){
              $('#someDiv').append(data); // appends your select list to some div
      
              /* after <select> is appended */
              var parent = '1';
              $('#parent').val(parent); // selects "Second" option
          })
      })
      

      【讨论】:

      • 哈哈。 thx 快速回复伙伴,但我们正在谈论 AJAX 生成的菜单
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2011-06-08
      • 1970-01-01
      • 2017-01-26
      • 2023-03-25
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多