0

我试图在一个按钮上触发一个 jquery 单击事件,该按钮通过 ajax 加载到一个 div 中,但是当我通过 ajax 加载按钮时,事件不会触发

破折号.php

<!DOCTYPE html>
<html>
 <head>
 <title>aquatrol</title> 
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<!-- Bootstrap -->
<script type="text/javascript" >
function dash_refresh() {

    var hr = new XMLHttpRequest();
    var url="dash_refresh.php";
    var stl="A";
    var vars="?STL="+stl;
    var url_full=url+vars+"&t=" + Math.random();

    hr.open("GET",url_full,true);

    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

   hr.onreadystatechange = function() {
       if(hr.readyState == 4 && hr.status == 200) {
           var return_data = hr.responseText;
            document.getElementById("dash-content").innerHTML = return_data;
       }
   }
    hr.send();
    document.getElementById("dash-content").innerHTML='<img src="img/ajax.gif" alt="Processing..." />';
}

setInterval(function(){
      dash_refresh();
},10000);
</script>
 <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/font-awesome.min.css" rel="stylesheet" media="screen"> 



<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
    <![endif]-->

<style type="text/css">
.btn-group[data-switch="true"] .btn.active {
font-weight: bolder;
}

</style>
</head>
 <body onload="javascript:dash_refresh();"> 
<a href="#" onclick="javascript:dash_refresh();" class="btn btn-primary pull-right">Refresh</a>
<hr>

<div class="container-fluid">
<div class="row-fluid" id="dash-content">
</div>
</div>



<script src="http://code.jquery.com/jquery.js"></script> 
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$("[data-switch='true']").on("click", ".btn", function() {
    var $this = $(this);


    if (!$this.hasClass("btn-info")) {
        $this.addClass("btn-info");
        var id=$this.attr('id');
    var value=$this.attr('value');
    alert("ID "+id+" Val "+value);

        $this.siblings().removeClass("btn-info");

    }
});
</script>


</body>
</html>

dash_refresh.php

<?
//some php here
?>

<div class="container-fluid">
<div class="row-fluid">

<div class="span12 well" style="text-align:center;">


<div class="btn-group" data-switch="true" data-toggle="buttons-radio">
    <button type="button" class="btn btn-small btn-info active" id="12" value="A">Auto</button>
    <button type="button" class="btn btn-small" id="12" value="1">On</button>               
    <button type="button" class="btn btn-small" id="12" value="0">Off</button>               

</div>

</div>
</div>
</div>

请帮我解决这个问题?

4

2 回答 2

3

您无法附加click处理程序,[data-switch='true']因为加载页面时它不存在。您必须将其附加到#dash-content

$('#dash-content').on('click', '[data-switch="true"] .btn', function() {
    // your code
});
于 2013-05-10T14:43:16.667 回答
3

首先,您所有的按钮都具有相同的 id id="12"。您正在触发点击这些按钮。这可能会导致问题。请更改它们。

<button type="button" class="btn btn-small btn-info active" id="12" ..
<button type="button" class="btn btn-small" id="12    ...            
<button type="button" class="btn btn-small" id="12"  .... 

这是副作用。对于点击事件问题,请查看@Billy 答案

于 2013-05-10T14:38:24.643 回答