jQuery 如何获取jquery变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17957933/
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-08-26 20:16:22  来源:igfitidea点击:

how to get a jquery variable value

jquery

提问by user86834

I have a jquery variable

我有一个 jquery 变量

$('#myValue')

In firebug if I hover over the variable at runtime I can see the the variable value "help". How can I get the the value "help" from the jquery variable and assign it to a javascript variable;

在萤火虫中,如果我在运行时将鼠标悬停在变量上,我可以看到变量值“帮助”。如何从 jquery 变量中获取值“help”并将其分配给 javascript 变量;

I have tried:

我试过了:

 var text = $('#myValue')

this jut creates an empty object

这 jut 创建了一个空对象

I have also tried

我也试过

  var text = $('#myValue').text()

Which returns an empty String "".

它返回一个空字符串“”。

And I have tried

我试过了

var text = $('#myValue').val()

Which returns undefined.

返回未定义。

回答by Frederik.L

I think this question has been clowning around for long enough. For the benefits of that question, I will clarify something: $('#myValue')is a jQuery selector that points toward an element with ID "myValue", just as it is used in css :

我认为这个问题已经流传了很长时间。为了这个问题的好处,我将澄清一些事情:$('#myValue')是一个指向ID 为“myValue”的元素的 jQuery 选择器,就像它在 css 中使用的一样:

#myValue { display:block;width:100px;height:100px; }

This line will instruct the page that the element with ID "myValue" needs to be displayed as a 100x100 block.

此行将指示页面 ID 为“myValue”的元素需要显示为 100x100 块。

Then,

然后,

var text = $('#myValue');

will hold the element with ID "myValue", if it ever exists. (IT WON'T GET THE VALUE OF ANY VARIABLE CALLED myValue)

将保存 ID 为“myValue”的元素,如果它存在的话。(它不会获得任何称为 myValue 的变量的值)

To make sure that the element exists, you may test it like this:

为了确保元素存在,你可以像这样测试它:

if ($('#myValue').length > 0) {
    // the element exists
}

There is no wizardry around jQuery selectors, you can take a look yourself at this reference. To get the text / value of an element you have many options depending on what the element is and what you want to access within it.

jQuery 选择器没有魔法,你可以自己看看这个参考。要获取元素的文本/值,您有许多选项,具体取决于元素是什么以及您想在其中访问什么。

  • Text inside a tag like div, span, etc. : $('#elementID').text();
  • Value of an option, input, button, etc. : $('#elementID').val();
  • All the html content within a tag : $('#elementID').html();
  • Etc.
  • 标签内的文本,如 div、span 等: $('#elementID').text();
  • 选项、输入、按钮等的值: $('#elementID').val();
  • 标签中的所有 html 内容: $('#elementID').html();
  • 等等。

If, however, you really want to get the value of a variable, I would ask you this: why do you need to put its value into another variable ?

但是,如果您真的想获取变量的值,我会问您:为什么需要将其值放入另一个变量中?

I hope this will clear things up.

我希望这能解决问题。

回答by Black Sheep

You can do:

你可以做:

1.) var text = $('#myValue').val();

1.) var text = $('#myValue').val();

OR

或者

2.) you get from variable like this:

2.) 你从这样的变量中得到:

var text = $('#myValue');

text.val();

回答by UserOnE

try this:

尝试这个:

var jqval = $('#myValue').val();

回答by nbrooks

jQuery makes it easy to select HTML elements and manipulate them from your JavaScript code. The jQuery function $can take a selector string as a parameter to select an element by ID.

jQuery 使选择 HTML 元素并从您的 JavaScript 代码中操作它们变得容易。jQuery 函数$可以将选择器字符串作为参数,通过 ID 选择元素。

For example, to select the HTML div element

例如,要选择 HTML div 元素

<div id="myValue">Some Text</div>

one would invoke $("#myValue"), which returns a jQuery object. This jQuery object contains, among other things, a collection of matched elements. Since we identified this element specifically by its ID, the collection should contain only this single element.

一个会调用$("#myValue"),它返回一个 jQuery 对象。这个 jQuery 对象包含匹配元素的集合等。由于我们通过其 ID 专门标识了此元素,因此该集合应仅包含此单个元素。

In practice, this can be used in any number of ways. To get the text content, you can invoke .text()on the jQuery object. To set an attribute, you can invoke .attr(name, value), and so on. The full method list is in the jQuery documentation.

实际上,这可以以多种方式使用。要获取文本内容,您可以调用.text()jQuery 对象。要设置属性,您可以调用.attr(name, value),依此类推。完整的方法列表在jQuery 文档中

For a simple test example, try this:

对于一个简单的测试示例,试试这个:

<!DOCTYPE html>
<html>
<head>
<script for jquery source>
<script> YOUR_CODE_HERE </script>
</head>
<body>
<div id="myValue">Some Text</div>
</body>
</html>

and in the script tag, the JS:

在脚本标签中,JS:

$(document).ready(function() {
    // get text
    var text = $("#myValue").text();
    alert(text);

    // make it blue
    $("#myValue").css("background-color", "LightBlue");
});

http://jsfiddle.net/CBZq7/

http://jsfiddle.net/CBZq7/