简体   繁体   English

使用jQuery通过数组索引和名称属性设置文本框值

[英]Set textbox value by array index and name attribute using jquery

$("[name=imageheight]") return following array of textboxes $(“ [name = imageheight]”)返回以下文本框数组

在此处输入图片说明

I know i can get the value of textbox by its index like 我知道我可以通过它的索引来获取文本框的值

在此处输入图片说明

How do i set the value of textbox on index 0. i have tried this but it gives me a error 我如何在索引0上设置文本框的值。我已经试过了,但这给我一个错误

在此处输入图片说明

$('[name=imageheight]').eq(0).attr('value', '250')

 $('input').eq(0).val("new value") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /> 

您可以使用

$('[name=imageheight]').eq(0).val(250)

Explanation: 说明:

jQuery returns a jQuery object which has a val function, which returns its value if no parameter is passed and sets its value if a parameter is passed. jQuery返回一个具有val函数的jQuery对象,如果不传递任何参数,该对象将返回其值;如果传递了一个参数,则将其设置为值。 However, the [0] of a jQuery object is an element and is not a jQuery object. 但是,jQuery对象的[0]是一个元素,而不是jQuery对象。 As a result, it does not have a val() function. 结果,它没有val()函数。 You can work with that as well, if you set its value attribute, like this: 如果您设置其value属性,也可以使用它,如下所示:

jQuery("input[name=firstname]")[0].value = 250; jQuery(“ input [name = firstname]”)[0] .value = 250; Also, you can bypass jQuery if you want, like this: 另外,您可以根据需要绕过jQuery,如下所示:

document.querySelectorAll("input[name=firstname]")[0].value = 250; document.querySelectorAll(“ input [name = firstname]”)[0] .value = 250;

The problem in your code is, $("[name=imageheight]")[0] will not return an jQuery object. 您的代码中的问题是, $("[name=imageheight]")[0]将不会返回jQuery对象。 So you cannot use .val() over that node. 因此,您不能在该节点上使用.val() But, $("[name=imageheight]").eq(0) will return the node as a jQuery object over which you can use .val() 但是, $("[name=imageheight]").eq(0)将返回该节点作为jQuery对象,可以在其上使用.val()

 $('input').eq(0).val("new value") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /> 

Actually $("[name=imageheight]") returns jquery object of that element. 实际上$(“ [name = imageheight]”)返回该元素的jquery对象。 that jquery object contains javascript DomElement object you can access DomElement object from jQuery object by $('....')[0] 该jquery对象包含javascript DomElement对象,您可以通过$('....')[0]从jQuery对象访问DomElement对象。

$("[name=imageheight]")[0].value obviously return value because value is propert of DomElement. $("[name=imageheight]")[0].value显然返回值,因为value是DomElement的属性。

You can set value by $("[name=imageheight]")[0].value= 250; 您可以通过$("[name=imageheight]")[0].value= 250;设置值$("[name=imageheight]")[0].value= 250; in DomElement object 在DomElement对象中

val() is setter and getter method of jQuery you have to fetch first element onlu by .first() method or ':first' selector. val()是jQuery的setter和getter方法,您必须通过.first .first()方法或':first'选择器来获取第一个元素onlu。

$("[name=imageheight]:first").val(250);

or 要么

$("[name=imageheight]").first().val(250);

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

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