javascript - 对ajax返回的选择值添加jquery onchange效果

标签 javascript jquery ajax combobox

好吧,我有一个从数据库返回值的组合框。这一切都是通过ajax 发生的。用户可以单击名为“添加新行”的按钮,将生成一个新行,该新行也将由组合框提供支持。 但是,我需要将“onchange”事件附加到 ajax 返回的组合框。

我尝试过:-

<script type="text/javascript">
$('.combobox').change(function() {
  var va = $('.combobox').val();
  alert(va);
});
</script>

但这没有用。我无法收到有关 onchange 事件的任何警报。

我必须尝试什么?
这是我的 ajax :-

try
{
  $s = $conn->query("SELECT * FROM testing2");
}
catch(PDOException $e)
{
  echo $e->getMessage();
}

$selectBegin = "";
$selectBegin = 
  '
  <select class="combobox">
    <option value=""></option>
    ';
    $innerSelectString = "";
$selectMain = "";
while($customers = $s->fetch(PDO::FETCH_OBJ))
{
  //fix the select thing here
  $si = "<option value='$customers->indexid'>$customers->v1</option>";
  $selectMain .= $si;
}
$selectEnd = '</select>';
$selectFinal = $selectBegin . $selectMain . $selectEnd;
return $selectFinal;

有多个组合框,我有这个 jquery 代码来运行组合框:-

function load_content_rows()
{
  $(document).ready(function() {
    $.ajax({    //create an ajax request to load_page.php
      type: "GET",
      url: "ajax.php?requestid=1",             
      dataType: "html",   //expect html to be returned                
      success: function(response){                    
        $("#requestMaker").html(response); 
        $("#requestMaker").find(".combobox").combobox();
        //alert(response);
      }
    }); //ajax request end
  });
} //load_content
load_content_rows();

如何将 onchange 事件添加到我的所有组合框? 它们的外观如下:- enter image description here

最佳答案

从 AJAX 选项中删除这部分,这可能会避免很多问题:

dataType: "html",   //expect html to be returned

因此您的 AJAX 请求将如下所示:

$(document).ready(function() {
  function load_content_rows() {
    $.ajax({                        //create an ajax request to load_page.php
      type: "GET",
      url: "ajax.php?requestid=1",             
      success: function(response){                    
        $("#requestMaker").html(response); 
        $("#requestMaker").find(".combobox").combobox();
                                    // alert(response);
      }
    });                             // ajax request end
  }                                 //load_content
  load_content_rows();
});

关于javascript - 对ajax返回的选择值添加jquery onchange效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32780639/

相关文章:

javascript - 我可以在 Javascript 中创建一个按顺序但不一定按顺序匹配字符的正则表达式吗?

jquery - 如何在 JQuery UI 对话框中设置元素的值

javascript - 如何在 firebase 的 authStateObserver 中调用 .isNewUser()

javascript - 防止 href ="#"链接更改 URL 哈希

javascript - 如何使用JQuery/JavaScript/CSS实现显示请求的命名 anchor

javascript - 滑动切换一旦点击就不会保持不动?

javascript - 生成按钮并使用构造函数添加方法

php - Javascript : Allow the user to write to session when submitting a form but without reloading the page

ruby-on-rails - 如何关闭 "periodically_call_remote"

javascript - 循环一个对象