jquery验证引擎 - 验证json列表

时间:2013-12-10 04:05:54

标签: javascript php jquery jquery-validation-engine

我有一个文本输入字段,使用ajax / php为用户设置为自动完成。虽然允许用户输入任何条目,但我想警告用户是否尝试输入不存在的值。

<input type="text" id="customerName" size="50" class="validate[required]" />
<input type="hidden" id="customerid"/>

现在我有一个针对该字段的自动完成:

$("#customerName").autocomplete({
    source: function (request,response) {
        $.ajax({
            url: 'getData.php?f=c&s=112',
            dataType: 'json',
            data: {
                term: request.term,
                country: $("#country").val()
            },
            success: function (data) {
                response(data)
            }
        })
    },
    minLength: 2,
    delay: 0,
    dataType: 'json',
    select: function( event, ui ) {
        $("#customerid").val(ui.item.id);
    }
});

我尝试使用blur()但它在很多实例上都有效,所以我想我会附加到验证引擎的钩子并检查哪个字段正在测试,所以我可以测试一下用户是否输入了什么通过测试customerid是否为空来判断是否为新。

form.bind('jqv.field.result', function(event, field, errorFound, promptText) {
    console.log('event = ' + event.toSource());
    console.log('field = ' + field.toSource());
    console.log('errorFound = ' + errorFound);
    console.log('promptText = ' + promptText);

我似乎无法弄清楚如何测试字段参数,看看我是否在合适的空间。

如果您能想到一个更容易/不同的方法,也可以随意发布,这只是我最初的想法。

1 个答案:

答案 0 :(得分:0)

我想我只需要睡个好觉。这是一个非常简单的解决方案:

form.bind('jqv.field.result', function(event, field, errorFound, promptText) {
    if (!errorFound)
        if (field.attr('name') === .... 

如果有错误,请让正常线程继续,因为存在本地错误。否则,我们可以检查名称字段,看看它是否是我们想要触发的字段,然后执行。