最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add onchange event on ajax? - Stack Overflow

programmeradmin8浏览0评论
<script type="text/javascript" src="//ajax.googleapis/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn").click(function() {
        var val = "Hi";
        $.ajax ({
            url: "1.php?id",
            data: { id : val }

        });
    });
});
</script>

<form name="numbers" id="numbers">
    <input type="text" name="id" id="btn">
<input type="text">
</form>

This code is for click event but i want onchange event like on select option. Can anybody suggest me how to add onchange event on this ajax code? Thank you in advance.

<script type="text/javascript" src="//ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#btn").click(function() {
        var val = "Hi";
        $.ajax ({
            url: "1.php?id",
            data: { id : val }

        });
    });
});
</script>

<form name="numbers" id="numbers">
    <input type="text" name="id" id="btn">
<input type="text">
</form>

This code is for click event but i want onchange event like on select option. Can anybody suggest me how to add onchange event on this ajax code? Thank you in advance.

Share Improve this question edited Feb 2, 2015 at 12:02 donald123 5,7593 gold badges28 silver badges24 bronze badges asked Feb 2, 2015 at 12:02 learnerlearner 211 gold badge1 silver badge4 bronze badges 2
  • Search here: api.jquery./category/events – yunzen Commented Feb 2, 2015 at 12:03
  • Where is select and option? – Manwal Commented Feb 2, 2015 at 12:03
Add a ment  | 

3 Answers 3

Reset to default 2
$(document).ready(function(){
    $("#btn").on("change", function() {
        var val = "Hi";
        $.ajax ({
            url: "1.php?id",
            data: { id : val }

        });
    });
});
<input type="text" name="id" id="btn" onchange="ajaxfunction()" />

<script>
function ajaxfunction()
{
    enter ajax code here
}
</script>

You can try using keyup like so.

$(document).ready(function () {
    $("#btn").keyup(function () {
        var val = $(this).val();
        $.ajax({
            url: "1.php?id",
            data: { id: val }

        });
    });
});
发布评论

评论列表(0)

  1. 暂无评论