【问题标题】:Ajax onchange is not workingAjax onchange 不起作用
【发布时间】:2014-03-26 10:11:58
【问题描述】:

我有一个 Ajax 函数来检查 username 是否可用。但是,如果我使用按钮并单击功能,它就可以工作。我不想使用按钮。因此,我正在检查更改 username2 字段的可用性。但这是行不通的。没有 Ajax 调用正在进行。

$('#user_name2').change(function(){

    //$(".help-block1").hide();
    var user_name = $("#user_name2").val();
    var name = $("#u_name").val();
    //var hidd = $("#hidd").val();
    var baseurl = $("#site_url").val();

    if (user_name == '') {
        $("#user_name2").after('<p class="help-block1"><b style="color:red;">Enter a username first</b></p>');
    }
    else if (user_name == name) {
        $("#user_name2").after('<p class="help-block1"><b style="color:red;">This is current username</b></p>');
    }
    else {

        $.ajax({
            url: "" + baseurl + "user/check_user",
            async: false,
            type: "POST",
            dataType: "json",
            cache: false,
            data: {
                    user_name: user_name
            },
            success: function(response) {
                if (response.res_user == 'exist') {

                    $("#user_name2").after('<p class="help-block1"><b style="color:red;">Username alredy exist</b></p>');
                    $("#user_name2").focus();
                    $("#hidd").val('fail');
                }
                else if (response.res_user == 'available') {
                   $("#user_name2").after('<p class="help-block1"><b style="color:' + response.status + ';">Username available</b></p>');
                }
                else {
                    $("#user_name2").after('<p class="help-block1"><b style="color:red;">Error. Try again</b></p>');
                }
            }
        });
        //return false;
    }
});

HTML 部分在这里

<div class="form-group">
    <label for="exampleInputEmail1">User Name</label>
    <input type="text" maxlength="255" class="form-control" id="user_name2" placeholder="" value="' . $res[0]->u_name . '"required>
    <input type="hidden" id="u_name" value="' . $res[0]->u_name . '">
</div>

【问题讨论】:

  • @MohammadAdil Adil :对不起...现在看到问题已编辑
  • 在文本字段上绑定更改事件监听器,您需要从输入文本元素失去焦点以发送 ajax 请求?
  • 什么意思?我无法理解。
  • 您是在文本域外点击吗?因为如果不是,您的 ajax 将不会触发。

标签: jquery ajax onchange


【解决方案1】:

根据我的理解,您将事件绑定到 click。我认为您应该在change 上绑定您的活动。

有可能,对你有帮助。

请使用

$(document).ready(function(){
  $('#username2').live('change', function() {});
});
/* put select box id at the place of username2 */

【讨论】:

  • 请查看附件图片,我认为它会产生错误。您必须将“username2”替换为您的元素 ID,您必须在其上绑定您的事件。
  • 我知道。它没有显示一个简单的错误。这是语法错误
  • 谢谢你,伙计。这不是那个问题。我没有对文本字段进行任何更改。这就是 $(document).ready(function() 不起作用的原因。
  • $(document).ready(function() 应该是 $(document).ready(function() {}); 并且请检查您的 jquery 版本,因为一些新的 jquery 不支持 @987654326 @.如果你的jquery版本大于等于1.7,那么你可以使用onOn方法是替换live。有关on的信息请访问click here
【解决方案2】:

使用 .on 代替 .live

$(document).ready(function() {

    $('#user_name2').on('change', function() {

    });
});

jQuery 1.9 .live() is not a function

演示:http://jsfiddle.net/avmCX/19/

注意:我测试了你的 siteurl 是未定义的,所以看看你在你的问题中发布的部分的代码

【讨论】:

  • 仍然没有.. 用 HTML 更新了我的问题。请检查
  • @SpamBox 什么不起作用?查看您的 Web 控制台,让我们知道正在记录哪些错误。
  • 我已经附上了演示,请通过它直到 ajax 调用,所以我猜你的 ajax 调用或服务器端有一些问题,因为直到现在它像你想要的那样工作@SpamBox
  • 感谢您的帮助@Neel
【解决方案3】:

我建议这样做:

$(document).ready(function() {
    $('#user_check').change(function() {
        // your code here
    });
});

使用$('#user_check')$('#user_name2'),具体取决于您在编辑问题后在HTML 中设置的id

还有

我认为您的HTML 无效。

你应该修复

value="' . $res[0]->u_name . '"

value="<?php echo $res[0]->u_name; ?>"

在你的&lt;input&gt;

【讨论】:

  • 问题有误。用实际代码更新了问题。它不工作
  • @SpamBox 是的,我刚看到它。顺便说一句,现在试试$(document).ready 以及
  • 仍然没有.. 用 HTML 更新了我的问题。请检查
  • html 没问题。它在 php echo 里面。
猜你喜欢
  • 2017-02-11
  • 2017-05-21
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 2011-02-02
  • 2015-02-05
  • 1970-01-01
相关资源
最近更新 更多