简体   繁体   English

jQuery Validation插件,用于动态元素和ajax

[英]jQuery Validation plugin for dynamic elements and ajax

I tried using the answer from this question , with no success. 我尝试使用此问题的答案, 没有成功。 Below is a shortened version of my HTML. 以下是我的HTML的简化版本。 If a User creates an Order with 6 routers, for example, when another User goes to create a Shipment for the Order, I create 6 rows in the table so the User can scan the router's barcode. 例如,如果一个用户用6个路由器创建了一个订单,那么当另一个用户去为该订单创建一个货件时,我会在表中创建6行,以便该用户可以扫描路由器的条形码。 The barcodes are validated through an ajax call. 条形码通过ajax调用进行验证。 If everything is good, the api will return "OK". 如果一切正常,则api将返回“确定”。 I want to make it so that the barcode input is invalid if the api result is not "OK". 我要这样做,以便如果api结果不是“ OK”,则条形码输入无效。

<form name="new_shipment" action="/shipments" method="post" id="shipment">
<table id="scan">
    <thead>
        <tr>
            <th>#</th>
            <th>Barcode</th>
            <th>Part Number</th>
            <th>Success</th>
        </tr>
    </thead>
    <tbody>
        <?php
            foreach ($this->scan_items as $k => $item) 
            {

                $count = $item['quantity'];
                for ($i=0; $i < $count; $i++) 
                {
        ?>
                    <tr>
                        <td><?= $i + 1 . '.'; ?></td>
                        <td><input class="scan_item" type="text" name="items[<?= $item['id']; ?>][]" value="" required></td>
                        <td><?= $item['part_number']; ?></td>
                        <td class="result_cell"><input type="text" class="api_message" name="api_message" value="" disabled></td>
                    </tr>
        <?php 
                }
            }            
        ?>
    </tbody>
</table>
</form>

For my Javascript, I grab the serial number from the input box, send an ajax call, and put the result in the last cell for the row. 对于我的Javascript,我从输入框中获取序列号,发送ajax调用,然后将结果放在该行的最后一个单元格中。 The input box that the result goes into is disabled, for obvious reasons. 由于明显的原因,结果进入的输入框被禁用。

// Validate shipment form, but get rid of messages
var validator = $("#shipment").validate({
    errorPlacement: function() {
        return false;
    },
    submitHandler: function() {
        sendAjax('/shipments',['shipment']);    
    }
});
$('.scan_item').on('keypress', function(event) {
    if (event.which == 13) {
        $(this).closest('tr').next().find('.scan_item').focus();
        var cell = $(this).closest('tr').find('.api_message');
        var sn = $(this).val();
        sendAjax('/check_sn&sn=' + sn, null, function(data) { 
            cell.val(data);
        });
    }
});

What is the best way to make the first input invalid based on the input from the second input box, that is disabled? 根据第二个输入框(已禁用)的输入,使第一个输入无效的最佳方法是什么? I cannot make the result input box required, because it is disabled, so it will be ignored . 我不能将结果输入框设为必需,因为它已被禁用,因此将被忽略

Since your barcode validation process is specific, a custom validation function (what you've stated in your question) might be the best solution. 由于条形码验证过程是特定的,因此自定义验证功能(您在问题中已说明的内容)可能是最好的解决方案。 However, jQuery Validator does offer the ability to validate a field remotely (via AJAX). 但是, jQuery Validator确实提供了通过AJAX远程验证字段的功能。

The plugin has a validation rule called remote that accomplishes much of what you're doing. 该插件有一个称为remote的验证规则,可以完成您正在执行的大部分工作。 There is a bit of custom coding involved, but not to the extent that the plugin no longer serves a purpose. 涉及到一些自定义编码,但不会超出该插件不再起作用的程度。

Take a look at this fiddle . 看看这个小提琴

It won't work since the '/check_sn/' endpoint doesn't exist, but I got it to work with the provided '/echo/json/' endpoint and a proxy program for altering the response. 由于'/ check_sn /'端点不存在,因此它不起作用,但是我将其与提供的'/ echo / json /'端点以及用于更改响应的代理程序一起使用。

Take notice of the errorPlacement and success properties passed to the .validate() function. 请注意传递给.validate()函数的errorPlacementsuccess属性。

...
errorPlacement: function(error, element) {
    //only display errors for the barcodes
    if ($(element).hasClass('scan_item')) {
        var $apiMessage = element.parents('tr').find('.result_cell');
        //show error in 'success' column
        error.appendTo($apiMessage);
    }
},
success: function(label, element) {
    if ($(element).hasClass('scan_item')) {
        //show 'OK' status in success column
        label.text('OK');
    }
}
...

I've listed some pros and cons to this approach. 我已经列出了这种方法的利弊。

PROS: 优点:

  • Uses the existing validator plugin (no custom validation script) 使用现有的验证器插件(无自定义验证脚本)

CONS: 缺点:

  • Uses a label element to display the error rather than an input element 使用label元素显示错误而不是input元素
  • Hiding other validation errors (eg required) would require some filtering 隐藏其他验证错误(例如,必填)将需要进行一些过滤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM