2017-08-28 200 views
-1

如何通過按鈕的onclick函數處理jQuery的事件如何通過按鈕處理jQuery的事件的onclick功能

HTML代碼

<div id="comments" class="cmt" > 
    <img width="30px" height="30px" src="/cropphoto/<?php echo getuserPhoto($d['uid']); ?>"> 
    <input class="commentbox" id="comment" name="comments" placeholder="Comment Here" maxlength="50" > 
    <input type="hidden" id="qid" name="qid " value="<?php echo $d['qid'];?>"> 
    <button type="button" id="comq" name="compost" onclick="my(event,<?php echo $d['qid'];?>);" class="butn2" >post comment</button> 
</div> 

AJAX JQUERY

$(document).ready(function() { 
    $("#comq").click(function my(e, id) { 
    var comment = $("#comment").val(); 
    var qid = $("#qid").val(); 

    alert(qid); 

    $.ajax({ 
     cache: false, 
     type: "post", 
     url: "jquery.php", 
     data: { 
     comments: comment, 
     qid: qid 
     }, 
     success: function(data) { 
     $("#comment").val(""); 
     $("").html(data); 
     } 
    }); 
    }); 

}); 

評論沒有插入通過點擊功能按鈕進入數據庫,但沒有按鍵w工作會有細微什麼是錯在我的代碼,請讓我知道我現在是在阿賈克斯

+0

哪裏是我的函數 – Gardezi

+0

@Gardezi看到AJAX請讓我知道我做了什麼錯誤,我在阿賈克斯新,jQuery的 –

+0

是的,我看到了它。你正在調用一個函數''onclick =「my(event,<?php echo $ d ['qid'];?>);」onclick。但我沒有看到它在任何地方定義 – Gardezi

回答

0

有兩個問題。

首先,onclick只能調用全局範圍內的函數。你的函數在$(document).ready()內定義,所以它不在全局範圍內。

其次,您正在使用命名函數表達式定義函數。這個名稱的範圍只是函數本身的主體。請參閱Why JavaScript function declaration (and expression)?

由於您從onclick調用該函數,因此無需將其放入$("#comq").click()$(document).ready()

只需將其定義爲全局範圍內的普通函數即可。而且,由於您使用$d['qid']作爲第二個參數來調用它,因此不需要在該函數中分配給qid。您也不需要event參數,因爲您從不在函數中使用它(因爲您從$("#comq").click()調用函數,所以以前只需要它)。

您不能多次使用相同的ID,ID的選擇器只會找到具有該ID的第一個元素,而不是該按鈕旁邊的元素。將該按鈕作爲函數的另一個參數傳遞,並使用它來獲取與其相鄰的評論框。

HTML:

<div id="comments" class="cmt" > 
    <img width="30px" height="30px" src="/cropphoto/<?php echo getuserPhoto($d['uid']); ?>"> 
    <input class="commentbox" name="comments" placeholder="Comment Here" maxlength="50" > 
    <button type="button" name="compost" onclick="my(<?php echo $d['qid'];?>, this);" class="butn2" >post comment</button> 
</div> 

JS:

function my(qid, button) { 
    var comment = $(button).siblings(".commentbox").val(); 

    alert(qid); 

    $.ajax({ 
    cache: false, 
    type: "post", 
    url: "jquery.php", 
    data: { 
     comments: comment, 
     qid: qid 
    }, 
    success: function(data) { 
     $("#comment").val(""); 
     $("").html(data); 
    } 
    }); 
} 

以下是一個演示它沒有AJAX可執行的代碼片段:

function my(qid, button) { 
 
    var comment = $(button).siblings(".commentbox").val(); 
 

 
    console.log("id = " + qid, " comment = " + comment); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="comments" class="cmt"> 
 
    <img width="30px" height="30px" src="/cropphoto/<?php echo getuserPhoto($d['uid']); ?>"> 
 
    <input class="commentbox" name="comments" placeholder="Comment Here" maxlength="50"> 
 
    <button type="button" name="compost" onclick="my(1, this);" class="butn2">post comment</button> 
 
</div> 
 
<div id="comments" class="cmt"> 
 
    <img width="30px" height="30px" src="/cropphoto/<?php echo getuserPhoto($d['uid']); ?>"> 
 
    <input class="commentbox" name="comments" placeholder="Comment Here" maxlength="50"> 
 
    <button type="button" name="compost" onclick="my(2, this);" class="butn2">post comment</button> 
 
</div>

+0

@Barmar請檢查這個鏈接工作正常與按鍵與按鈕,但我們需要按鈕點擊檢出https://pastebin.com/6NvqYfqu –

+0

我的代碼段顯示,按鈕點擊也可以。我不知道爲什麼它不適合你。 – Barmar

+0

@have你見過我的鏈接我發給你的工作正常 –

0

首先它看起來像你要發送2個參數在你的HTML:

onclick="my(event,<?php echo $d['qid'];?>); 

但功能只接受1?

$("#comq").click (function my(e) { 

我不確定這是否解決了您的問題,但您可以從那裏開始。

+0

是需要接受1,但如何接受所​​有 –

+0

你可以讓它接受1更多的參數: '$(「#comq」)。click(function my(e,param){' – kuylis

+0

e和param現在是您的2個參數,使用param來獲取您嘗試從您的HTML代碼發送的值。 – kuylis

0

哦了它只是添加其他參數與它

$("#comq").click (function my(e, id) { 

或什麼,我會做的是我會定義一個函數我這樣

function my(e, id){ 
$.ajax({ 
     cache:false, 
     type:"post", 
     url:"jquery.php", 
     data:{$("#comment").val(),qid:id}, 
     success:function(data) 
     { 
        $("#comment").val(""); 
        $("").html(data); 

     } 
} 

這看起來在我看來更整潔

0

首先,你不需要使用:

$("#comq").click (function my(e... 

,因爲你已經擁有的onclick(我的(A,B))。只需創建一個功能:

function my(a, b){ 
     alert(a +" "+b) 
} 

這是否適合您?

<html> 
<body> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<div id="comments" class="cmt" > 
    <img width="30px" height="30px" src="/cropphoto/<?php echo getuserPhoto($d['uid']); ?>"> 
    <input class="commentbox" id="comment" name="comments" placeholder="Comment Here" maxlength="50" > 
    <button type="button" id="comq" name="compost" onclick="my(2, 'test test')" class="butn2" >post comment</button> 

</div> 

<script> 
    function my(comment, qid){ 

     alert(comment + " - " + qid) 
     $.ajax({ 
      cache:false, 
      type:"post", 
      url:"jquery.php", 
      data:{comments:comment,qid:qid}, 
      success:function(data) 
      { 
       $("#comment").val(""); 
      } 
     }); 
    } 
</script> 

</body> 
</html> 
+0

得到類似於對象鼠標事件的提醒qid no –

+0

您的參數順序錯誤。 – Barmar

+0

@Barmar請改進granit回答它的小錯誤 –