我想使用ajax调用传递按钮的id ...并将结果显示为名为demo2的id ...
<script>
function loadDoc2() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo2").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
xhttp.send();
}
</script>
<div id="demo2"></div>
<button id="111" onclick="loadDoc2()">Button 1</button>
<button id="222" onclick="loadDoc2()">Button 2</button>
<button id="333" onclick="loadDoc2()">Button 3</button>
答案 0 :(得分:0)
我会在函数上传递它并将其用作变量,因为“this”看起来在窗口范围内。
<script>
function loadDoc2(element) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo2").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +element.id, true);
xhttp.send();
document.getElementById("demo2").innerHTML = element.id + " clicked";
}
</script>
<div id="demo2"></div>
<button id="111" onclick="loadDoc2(this)">Button 1</button>
<button id="222" onclick="loadDoc2(this)">Button 2</button>
<button id="333" onclick="loadDoc2(this)">Button 3</button>
答案 1 :(得分:0)
您可以通过两种方式完成此操作 没有。
<script>
function loadDoc2(id) { //here is your id
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo2").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
xhttp.send();
}
</script>
<div id="demo2"></div>
<button id="111" onclick="loadDoc2("111")">Button 1</button>
<button id="222" onclick="loadDoc2("222")">Button 2</button>
<button id="333" onclick="loadDoc2("333")">Button 3</button>
否2.
<script>
function loadDoc2() {
var Id = $(this).attr('id'); //here is your id
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo2").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "get_coupon_code.php?offline_offer_id=" +this.id, true);
xhttp.send();
}
</script>
<div id="demo2"></div>
<button id="111" onclick="loadDoc2()">Button 1</button>
<button id="222" onclick="loadDoc2()">Button 2</button>
<button id="333" onclick="loadDoc2()">Button 3</button>