jquery上的click事件不起作用

时间:2015-12-05 09:10:06

标签: javascript php jquery html

我用输入元素的php打印了一个echo,我需要使用jquery获取click事件id。以下是我想要的代码

API.php(使用ajax对index.php的echo元素):

echo"<input type='submit' id='something' value='accept me'/>";

index.php(需要在点击事件中获取id =&#39;

):

$("#something").click(function(){
    alert("hi");
    //doesnt work 
});

为什么它不起作用?请建议

4 个答案:

答案 0 :(得分:3)

onclick()本身上写input函数。它会工作。

有关详情,请点击Add Onclick On Submit Button - Stack Overflow

<form ... >
    .
    .
    <input type='submit' id='something' onclick="return showValue();" value='accept me'/>
</form>

<script>
function showValue() {
    alert('hi');
    return false;
}
</script>

答案 1 :(得分:1)

 window.onload=function(){
         $("#something").click(function(){
           alert("hi");
         });
      }

答案 2 :(得分:0)

试试这个:

$( document ).ready(function() {
    $("#something").click(function(){
    alert("hi");
    //doesnt work 
});
});

如果这不起作用,那么您需要显示您的代码。 http://jsfiddle.net/y6bgov3e/

答案 3 :(得分:0)

这是运行代码:

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<input type='button' id='test' value='accept me'/>

<script>
$(document).ready(function(){
    $('#test').bind('click', function(){
        alert('Hello')
    })
})
</script>

如果您想点击按钮,请使用提交的按钮类型

相关问题