简体   繁体   English

当我只知道它的“名称” - 属性时,如何通过jQuery设置input-element的值

[英]How do I set the value of an input-element via jQuery, when I only know it's “name”-attribute

I need to reset some text-boxes which only have an unique name (not an unique id). 我需要重置一些只有唯一名称(不是唯一ID)的文本框。 I tried it with jQuery, but my code seems to do nothing: 我用jQuery尝试过,但我的代码似乎什么也没做:

$('input[name=customergroupname]').value="";

尝试这个:

$('input[name="customergroupname"]').val("");

Getting and setting value of form elements wrapped in a jQuery object is being done with use jQuery val function: 使用jQuery val函数来获取和设置包装在jQuery对象中的表单元素的值:

$('input[name="customergroupname"]').val("");

.value can only be used for DOM elements, like this: .value只能用于DOM元素,如下所示:

$('input[name="customergroupname"]').eq(0).value ="";

$(...)       // This is a jQuery object.
$(...)[n]    // This is a DOM object in the nth place in the set.
$(...).eq(n) // This is a DOM object in the nth place in the set.

Working demo http://jsfiddle.net/vN74v/2/ 工作演示 http://jsfiddle.net/vN74v/2/

Code

$('#hullk').click(function(){ // in example when you click the button
    $('input[name="customergroupname"]').val("");
});
​

According to DOM level 2 specifications , you can access: 根据DOM 2级规范 ,您可以访问:

document.forms["foo"].elements["customergroupname"].value = '';

If formName and fieldName are constants rather than variables you can use literal syntax: 如果formNamefieldName是常量而不是变量,则可以使用文字语法:

document.forms.foo.elements.customergroupname.value = '';

try: 尝试:

$('input[name="customergroupname"]').value="";

I've wrapped customergroupname in quotes. 我把customergroupname包装在引号中。

Very true, I mixed the quotes up. 非常正确,我把报价混在一起。 Edited now. 现在编辑。

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

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