2017-08-28 92 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回答它的小错误 –