简体   繁体   English

无法使用jQuery操作DOM元素

[英]Unable to manipulate DOM element with jQuery

I've run into a very strange situation. 我遇到了一个非常奇怪的情况。 I have a simple registration form which has an input box to validate a registration. 我有一个简单的注册表格,其中包含一个用于验证注册的input框。 The whole site is essentially one large stylized form . 整个站点实质上是一种大型的程式化form In my mockup ( CodePen demo ), everything works fine. 在我的模型( CodePen demo )中,一切正常。 If the key matches, the input background is shaded green. 如果键匹配,则输入背景为绿色阴影。

HTML 的HTML

<div id="workshops">
  <h1>Upcoming Workshops</h1>
  <p>To register for a workshop, fill in your name information and then click on the button next to each session title. Click <b>Register</b> when you're finished.</p>
  <div id="form">
    <form id="classSignup" class="noSubmit" onsubmit="handleFormSubmit(this)">
      <input id="email" name="email" type="hidden" value="<?= Session.getActiveUser().getEmail(); ?>" />
      <input id="first" name="first" type="text" onfocus="this.value=''" value="First Name" required />
      <input id="last" name="last" type="text" onfocus="this.value=''" value="Last Name" required />
      <select id="bldg" name="building"></select>
      <input type="submit" value="Register" />
      <div id="list">
        <div class='row' id='row9'>
          <span class='time'>8:00</span>
          <span class='date'>6/20/2017</span>
          <input type='checkbox' name='wkshp' value='6/20/2017'/>
          <span class='title'>
            <label for='wkshp'>Demo workshop</label>
          </span>
          <span class='desc'>This is the class description</span>
          <div class='meta'>
            <span class='loc'>Admin building</span> | 
            <span class='cat'>1a, 2b, 2c, 3d</span> | 
            <span class='type'>Online</span> | 
            <span class='seats'>Seats: 15</span>
          </div>
          <label>
            <input type='text' class='lock' name='regCode9' value='Code' />
          </label>
        </div>
      </div>
    </form>
  </div>
</div>

Example Script 示例脚本

$("div.lock").keyup(function() {
  if( $(this).val() == "abc") {
    $(this).css('background-color', 'rgba(0, 255,0,0.4)')
  } else {
    $(this).css('background-color', 'white')
  }
})

When I move this over to my live site, I can't access the input element with jQuery. 当我将其移至实时站点时,无法使用jQuery访问input元素。 I can find it in the DOM and edit CSS in the Inspector, but regardless of what I do, I can't even get an error message to log in the console. 我可以在DOM中找到它,并在Inspector中编辑CSS,但是无论我做什么,我什至都无法获得错误消息来登录控制台。 I have plenty of other scripts running with no problems on the page. 我还有许多其他脚本在页面上正常运行。

I'm really at a loss as to why it works in one site but not another. 我真的不知道为什么它可以在一个站点而不是另一个站点工作。 Any ideas are appreciated. 任何想法表示赞赏。

Try delegating the keyup event 尝试委派keyup事件

$("body").on("keyup","input.lock", function() {
  if( $(this).val() == "abc") {
    $(this).css('background-color', 'rgba(0, 255,0,0.4)');
  } else {
    $(this).css('background-color', 'white');
  }
});

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

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