jQuery Jquery在div内的列表中获取输入值

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

Jquery get input value inside list which is inside a div

jquery

提问by McNabbToSkins

I have something like the following

我有类似以下的东西

<div id="container">
      <ul id="name_list">
          <li class="name">
             <input type="hidden" class="name_val" value="5" />
          </li>
      </ul>
</div>

I am trying to get the value of the input. So far I have the following Jquery statment.

我正在尝试获取输入的值。到目前为止,我有以下 Jquery 语句。

$("#container li.name input.name_val").val();

this does not work anyone know a better way?

这不起作用有人知道更好的方法吗?

回答by rahul

You were having a typo in your input class name

你的输入类名有错别字

$("#container li.name input.name_val").val();

Also you can change your selector to something like

您也可以将选择器更改为类似

$("#name_list > li.name > input:hidden.name_val").val();

回答by dnagirl

Give your <input>either a name or an id. The you can get the value in either of these two ways:

给你<input>一个名字或一个id。您可以通过以下两种方式之一获取值:

$('input[name=thename]').val()

or

或者

$('input#theid').val()

回答by Chris Barry

Should it be

应该是

var value = $('input.name_val').val();

You don't need to mention the other parts of the dom, just the area you are interested in.

你不需要提到dom的其他部分,只需要提到你感兴趣的领域。