具有多个提交按钮的Ajax [英] Ajax with multiple submit buttons

查看:115
本文介绍了具有多个提交按钮的Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改下面的代码,而不是使用带有提交按钮的文本输入类型,我希望多个提交按钮各自具有自己的唯一值?我尝试的一切最终都以未定义Submit的值结束.任何帮助都会很棒!

How can I change the code below so instead of a text input type with a submit button I want multiple submit buttons each with their own unique value? Everything I try just ends up with submit's value being undefined. Any help would be great!

代码源:提交搜索查询&无需刷新即可获取搜索结果

<script type="text/javascript">
    $(function() {
        $("#lets_search").bind('submit',function() {
            var value = $('#str').val();
            $.post('db_query.php',{value:value}, function(data){
                $("#search_results").html(data);
            });
            return false;
        });
    });
</script>

<form id="lets_search" action="" >
    Search:<input type="text" name="str" id="str">
    <input type="submit" value="send" name="send" id="send">
</form>

推荐答案

您可以添加多个提交按钮并将它们附加到所有onclick事件侦听器.单击按钮时-获取值并发送POST请求.

You can add multiple submit buttons and attach to all of them onclick event listener. When button was clicked - get the value and send with a POST request.

<script>
$(function(){
    $('input[type=submit]').click(function(){
        $.post('db_query.php', {value:$(this).val()}, function(data){
            $("#search_results").html(data);
        });
        return false;
    });
});
</script>
<form id="lets_search" action="">
    <input type="submit" name="button1" value="hi"/>
    <input type="submit" name="button2" value="bye"/>
</form>

这篇关于具有多个提交按钮的Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆