javascript jQuery $(this).val(); 在 .ready 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17233797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:34:21  来源:igfitidea点击:

Jquery $(this).val(); on .ready not working

javascriptjqueryselected

提问by mitt

I'm trying to get the value of a dropdown's option (there is an id on the select markup), when opening the web page

我试图在打开网页时获取下拉选项的值(选择标记上有一个 id)

Using

使用

$(document).ready(function() {

  $('#cat_list').ready(function(){
  var category =  $(this).val();
  alert(category);
  });

});

I get a blank alert.

我收到一个空白警报。

But Using .change (when selecting something else inside the dropdown) the following code works perfectly with the same function

但是使用 .change(在下拉列表中选择其他内容时)下面的代码可以完美地使用相同的功能

$(document).ready(function() {

  $('#cat_list').change(function(){
  var category =  $(this).val();
  alert(category);
  });

});

Finally, this works using basic javascript and it gets successfully the values on open, refresh, on form submit fail, ... etc

最后,这使用基本的 javascript 工作,并成功获取打开、刷新、表单提交失败等值

$(document).ready(function() {

  $('#cat_list').ready(function(){
  var e = document.getElementById("cat_list");
  var category = e.options[e.selectedIndex].value; 
  alert(category);
  });

});

Thanks for any help on why the first version .ready + $(this).val(); fails

感谢您帮助解释为什么第一个版本 .ready + $(this).val(); 失败

回答by A. Wolff

Correct code is:

正确的代码是:

$(document).ready(function () {
    var category = $('#cat_list').val();
    alert(category);
});

回答by dev2d

$(document).ready itself means the whole document (including #cat_list) is ready to be processed. why are you checking if an element is ready or not!!??

$(document).ready 本身意味着整个文档(包括#cat_list)已准备好进行处理。你为什么要检查一个元素是否准备好!??

you can directly use the value of the element like

您可以直接使用元素的值,例如

$('#cat_list').val();

$('#cat_list').val();

回答by claustrofob

The documentation says that .ready:

文档说.ready

Specify a function to execute when the DOM is fully loaded.

指定在 DOM 完全加载时要执行的函数。

And 3 possible usage cases are:

3 种可能的用例是:

  • $(document).ready(handler)
  • $().ready(handler) (this is not recommended)
  • $(handler)
  • $(document).ready(handler)
  • $().ready(handler)(不推荐这样做)
  • $(处理程序)

However you can actually assign .readyto any element and it will be triggered:

但是,您实际上可以分配.ready给任何元素,它会被触发:

$('#cat_list').ready(function(){

});

This code is fired. BUTthisinside .readyfunction always refers to document.

这段代码被触发了。但是this内部.ready函数总是指document.

It will work this way:

它将以这种方式工作:

$(document).ready(function() {
  $('#cat_list').ready(function(){
      var category =  $('#cat_list').val();
      alert(category);
  });
});

But actually your code is overengineered:

但实际上你的代码被过度设计了:

$(document).ready(function() {
    var category =  $('#cat_list').val();
    alert(category);
});