我有一个功能
(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;
}
};'
并尝试在 foreach php 代码中运行它:
<script>
jQuery("#1133156434").field('field1', 'kdweb');
</script>
我收到消息:
TypeError: jQuery(...).
字段不是函数错误源行:
jQuery("#1133156434").field('field1', 'kdweb');
那么我应该在哪里粘贴函数的定义,或者我应该在document ready {}
. 现在我在 html 文件的开头有这个功能