简体   繁体   English

无法使用jQuery类选择器选择所有元素

[英]Unable to select all elements using jQuery class selector

Here iam having a table data in which 3 input items which are having a common class submit3 .Whenever i want to select the 3 buttons using jQuery and want to change the value and name attributes of these elements to some other text. 这里的iam有一个表数据,其中3个输入项具有一个公共类Submit3。每当我想使用jQuery选择3个按钮并想将这些元素的value和name属性更改为其他文本时。

Here iam including the code snippet of the data 这里的iam包括数据的代码段

<td colspan=2>

    <input type="submit" value="Login" class="submit3" onclick="return checknull()" tabindex="1">

    <input type="button" value="Reset" class="submit3" onclick="call_reset()" tabindex="1">

    <input type="button" value="Forgot Password ?" class="submit3" onclick="call_fgpwd()" tabindex="1">

</td>

But when iam selecting with jquery using the class selector $('.submit3') it only returning the first input submit button and it is ignoring the remaining 2. 但是,当iam使用类选择器$('。submit3')与jquery选择时,它仅返回第一个输入的提交按钮,而忽略其余的2。

Here original javascript is here 这里原始的JavaScript在这里

document.getElementsByClassName("submit3")[0].value = "something";
document.getElementsByClassName("submit3")[1].name = "something";
document.getElementsByClassName("submit3")[2].name = "something";
document.getElementsByClassName("submit3")[2].value = "something";

Now i tried converting it into jquery as 现在我尝试将其转换为jquery

$('.submit3')[0].val("Something New")

Which resulted in error Uncaught TypeError: $(...)[0].val is not a function 导致错误Uncaught TypeError: $(...)[0].val is not a function

Also in the console of chrome browser(Latest Version),when i run $('submit3') the output is <input type="submit" value="Login" class="submit3" onclick="return checknull()" tabindex="1"> 同样在chrome浏览器的控制台(最新版本)中,当我运行$('submit3')时,输出为<input type="submit" value="Login" class="submit3" onclick="return checknull()" tabindex="1"> 在此处输入图片说明

In your code $('.submit3')[0] returns the dom object, you can't use jQuery val() method with it. 在您的代码$('.submit3')[0]返回dom对象中,您不能使用jQuery val()方法。 You can set value property to update the value. 您可以设置value属性来更新值。

$('.submit3')[0].value = "Something New"

or use eq() method to get jQuery object using index 或使用eq()方法使用索引获取jQuery对象

$('.submit3').eq(0).val("Something New")
// or $('.submit3:eq(0)').val("Something New")

When you convert a jQuery collection $('.submit3') to $('.submit3')[0] - raw DOM element [HTMLElement] , the jQuery properties don't work. 当您将jQuery集合$('.submit3')$('.submit3')[0] -原始DOM元素[HTMLElement] ,jQuery属性不起作用。 Use plain JS properties. 使用普通的JS属性。

$( ".foo" )[ 0 ] // is equivalent to document.getElementsByClassName( "foo" )[0]

Use 采用

$('.submit3')[0].value = ...

or alternatively 或者

$('.submit3').get(0).value = ..

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

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