如何实现和运行这个jQuery函数,并在php代码中运行该函数


How to implement and run this jQuery function and run that function in php code

我有一个函数

(function () {jQuery.fn.field = function (inputName, value) {
    if (typeof inputName !== "string") return false;
    var $inputElement = jQuery(this).find("input[name*=" + inputName + "]");


    if (typeof value === "undefined" && $inputElement.length >= 1) {
        switch ($inputElement.attr("type")) {
            case "checkbox":
                return $inputElement.is(":checked");
            case "radio":
                var result;
                $inputElement.each(function (i, val) {
                    if (jQuery(this).is(":checked")) result = $(this).val();
                });
                return result;
            default:
                return $inputElement.val();
        }
    } else {
        switch ($inputElement.attr("type")) {
            case "checkbox":
                $inputElement.attr({
                    checked: value
                });
                break;
            case "radio":
                $inputElement.each(function (i) {
                    if (jQuery(this).val() == value) $(this).attr({
                        checked: true
                    });
                });
                break;
            case undefined:                   
            break;
            default:
                $inputElement.val(value);
                break;
        }
        return $inputElement;
    }
};'

并尝试在每个PHP代码中运行它:

<script>


                                    jQuery("#1133156434").field('field1', 'kdweb');
                                    </script>

我收到消息:

TypeError: jQuery(...).字段不是函数错误源行:

jQuery("#1133156434").field('field1', 'kdweb');

那么我应该在哪里粘贴函数的定义,或者我应该在 document ready {} 中运行它。现在我在html文件的开头有这个功能

您收到此异常是因为您未正确使用该函数。

我不明白你想做什么...但是像这样使用函数:

field('field1', 'kdweb');