jQuery jquery中如何定义变量

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

how to define variable in jquery

jqueryvariables

提问by Tomas Aschan

I would like to know how to declare a variable in jQuery

我想知道如何在 jQuery 中声明一个变量

The code I am currently using is

我目前使用的代码是

$.name = 'anirudha';
alert($.name);

That code works fine, but if I write it as

该代码工作正常,但如果我把它写成

$.name = document.myForm.txtname.value;
alert($.name);

Then my code does not work.

然后我的代码不起作用。

回答by Tomas Aschan

jQuery is just a javascript library that makes some extra stuff available when writing javascript - so there is no reason to use jQuery for declaring variables. Use "regular" javascript:

jQuery 只是一个 javascript 库,它在编写 javascript 时提供了一些额外的东西 - 所以没有理由使用 jQuery 来声明变量。使用“常规”javascript:

var name = document.myForm.txtname.value;
alert(name);

EDIT: As Canavar points out in his example, it is also possible to use jQuery to get the form value:

编辑:正如 Canavar 在他的例子中指出的那样,也可以使用 jQuery 来获取表单值:

var name = $('#txtname').val(); // Yes, it's called .val(), not .value()

given that the text box has its idattribute set to txtname. However, you don't needto use jQuery just because you can.

鉴于文本框的id属性设置为txtname。但是,你没有需要使用jQuery只是因为你可以

回答by Canavar

Try this :

尝试这个 :

var name = $("#txtname").val();
alert(name);

回答by Jquery

Here's are some examples:

以下是一些示例:

var name = 'india';
alert(name);  


var name = $("#txtname").val();
alert(name);

Taken from http://way2finder.blogspot.in/2013/09/how-to-create-variable-in-jquery.html

取自 http://way2finder.blogspot.in/2013/09/how-to-create-variable-in-jquery.html

回答by Than Htut Win

In jquery, u can delcare variable two styles.

在 jquery 中,您可以将变量声明为两种样式。

One is,

一是,

$.name = 'anirudha';
alert($.name);

Second is,

其次是,

var hText = $("#head1").text();

Second is used when you read data from textbox, label, etc.

当读取数据二是使用textboxlabel等等。

回答by Bhaskarreddy Mule

in jquery we have to use selector($) to declare variables

在 jquery 中我们必须使用 selector($) 来声明变量

var test=$("<%=ddl.ClientId%>");

here we can get the id of drop down to j query variable

在这里我们可以得到下拉到j 查询变量的 id

回答by A Shelton

You can also use text()to set or get the text content of selected elements

也可以text()用来设置或获取被选元素的文本内容

var text1 = $("#idName").text();

回答by Saugat Shrestha

var name = 'john';
document.write(name);

it will write the variableyou have declared upper

它将写入您已声明的变量upper

回答by Alejandro Nava

Remember jQuery is a JavaScript library, i.e.like an extension. That means you can use both jQuery and JavaScript in the same function (restrictions apply).

记住 jQuery 是一个 JavaScript像一个扩展。这意味着您可以在同一个函数中同时使用 jQuery 和 JavaScript(有限制)。

You declare/createvariables in the same way as in Javascript: var example;

您以与在 Javascript 中相同的方式声明/创建变量:var example;

However, you can use jQuery for assigning values to variables:

但是,您可以使用 jQuery 为变量赋值

var example = $("#unique_product_code").html();

Instead of pure JavaScript:

而不是纯 JavaScript:

var example = document.getElementById("unique_product_code").innerHTML;