jquery上的click事件不起作用


click event on jquery does not work

我已经用php打印了一个输入元素的echo,我需要使用jquery获取点击事件id。下面是我试图获得的代码

API.php(使用ajax将元素回声到index.php):

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

php(需要在点击事件中获取id="something"):

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

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

input本身上编写onclick()函数。它会起作用的。

有关更多信息,请单击"添加"点击提交"按钮-堆栈溢出

<form ... >
    .
    .
    <input type='submit' id='something' onclick="return showValue();" value='accept me'/>
</form>
<script>
function showValue() {
    alert('hi');
    return false;
}
</script>
 window.onload=function(){
         $("#something").click(function(){
           alert("hi");
         });
      }

试试这个:

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

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

下面是运行代码:

<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>

如果你想点击按钮,那么使用按钮类型代替提交