简体   繁体   English

如何在 JavaScript/Jquery 中检索 ID 为“FullName”的 DIV 元素和 ID 为“FirstName”的 INPUT 字段的值

[英]How to retrive the value of a DIV element with ID “FullName” and an INPUT field with the ID “FirstName in JavaScript/Jquery

<div id="FullName">
  <input type="text" id="FirstName" value="Srinath"/>
  <input type="text" id="LastName" value="Reddy"/>
</div>

How to retrive the value of firstName of div1 I want to print the value Srinath.如何检索 div1 的 firstName 的值我想打印值 Srinath。

HTML ids are supposed to be unique, and you should always try not to duplicate them. HTML id 应该是唯一的,您应该始终尽量不要重复它们。 If you reeally want to do that you can do it with the css element in element selector like that:如果你真的想这样做,你可以使用元素选择器中的 css 元素来做到这一点:

$("#FullName1 #FirstName")

You can get the value like this:你可以得到这样的值:

 var firstName = document.getElementById('FirstName').value; alert(firstName);
 <div id="FullName"> <input type="text" id="FirstName" value="Srinath"/> <input type="text" id="LastName" value="Reddy"/> </div>

You can get it also by jQuery like this:您也可以通过 jQuery 获取它,如下所示:

$('#FirstName').val()

Please check this:请检查这个:

$("#FullName input#FirstName").val(Text); $("#FullName 输入#FirstName").val(Text);

If in your scenario, you always want to get value of first input contro l in your div having specific id, then you can use following如果在您的场景中,您总是希望在具有特定 id 的div获取第一个输入控件的值,那么您可以使用以下内容

$("#FullName>input").val()

Output : Srinath输出: Srinath

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

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