【问题标题】:jquery validation engine - validate against json listjquery 验证引擎 - 针对 json 列表进行验证
【发布时间】:2013-12-10 04:05:54
【问题描述】:

我有一个设置为使用 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);

我似乎无法弄清楚如何测试字段参数以查看我是否在正确的空间中。

如果您能想到更简单/不同的方法,请随时发布,这只是我最初的想法。

【问题讨论】:

  • 需要用引号结束$("#customerid").val(ui.item.id);
  • 这只是一个帖子错字,我没有剪切粘贴。

标签: javascript php jquery jquery-validation-engine


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多