简体   繁体   English

更改输入值的简单 jQuery 调用继续返回 TypeError $(...) 为 null

[英]Simple jQuery call to change input value continues returning TypeError $(...) is null

My hair is falling out while I'm trying to figure out why jQuery keeps throwing a TypeError for what I think is a valid call to replace an input value in the HTML.当我试图弄清楚为什么 jQuery 不断抛出 TypeError 时,我的头发掉了,我认为这是替换 HTML 中输入值的有效调用。

The line below is being called in one of my functions to change all of the input values for those with the name "form_key."在我的一个函数中调用下面的行来更改名称为“form_key”的所有输入值。

$('input[name="form_key"]').val("ItWorks!"); $('input[name="form_key"]').val("ItWorks!");

(resides in http://vt2.net/js/warp/warp.js ) (驻留在http://vt2.net/js/warp/warp.js

There are multiple pages that have this hidden input name and some pages have it more than once, and I'm trying to change the value for all on the page.有多个页面具有这个隐藏的输入名称,有些页面不止一次,我正在尝试更改页面上所有内容的值。

The page below only has one but Firebug keeps showing a TypeError no matter how many ways I try to replace the value.下面的页面只有一个,但无论我尝试用多少种方法替换该值,Firebug 都会一直显示 TypeError。 http://vt2.net/my-test-product.html http://vt2.net/my-test-product.html

It's probably something really simple and stupid but I keep missing it!这可能是非常简单和愚蠢的事情,但我一直想念它! Any ideas?有任何想法吗?

You already use你已经使用

http://vt2.net/js/lib/jquery/noconflict.js

where define在哪里定义

// Avoid PrototypeJS conflicts, assign jQuery to $j instead of $
var $j = jQuery.noConflict();

So for all operation by jQuery, you need use $j instead just $所以对于 jQuery 的所有操作,你需要使用$j而不是$

And your $ now from prototype.js你的$现在来自prototype.js

function $(element) {
  if (arguments.length > 1) {
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
      elements.push($(arguments[i]));
    return elements;
  }
  if (Object.isString(element))
    element = document.getElementById(element);
  return Element.extend(element);
}

Kevin B is right $ is not jQuery on http://vt2.net/my-test-product.html .凯文 B 是对的$不是http://vt2.net/my-test-product.html 上的jQuery

Use

jQuery('input[name="form_key"]').val("ItWorks!");

Or或者

jQuery.noConflict()

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

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