简体   繁体   English

jQuery 更改输入文本值

[英]jQuery change input text value

I can't find the right selector for:我找不到正确的选择器:

<input maxlength="6" size="6" id="colorpickerField1" name="sitebg" value="#EEEEEE" type="text">

I want to change the value to = 000000. I need the selector to find the "name" not the id of the text input.我想将值更改为 = 000000。我需要选择器来查找“名称”而不是文本输入的 id。

Shouldn't this work?:这不应该工作吗?:

$("text.sitebg").val("000000");

The presented solution does not work, what's the problem with this?提出的解决方案不起作用,这是什么问题?

$.getJSON("http://www.mysitehere.org/wp-content/themes/ctr-theme/update_genform.php",function(data) {
    $("#form1").append(data.sitebg);
    $('input.sitebg').val('000000');
});

The JSON data is working correctly; JSON 数据工作正常; the idea is to later pass the JSON values into the form input text values.这个想法是稍后将 JSON 值传递到表单输入文本值中。 But is not working :(但不起作用:(

no, you need to do something like:不,您需要执行以下操作:

$('input.sitebg').val('000000');

but you should really be using unique IDs if you can.但如果可以的话,你真的应该使用唯一的 ID。

You can also get more specific, such as:您还可以获得更具体的信息,例如:

$('input[type=text].sitebg').val('000000');

EDIT:编辑:

do this to find your input based on the name attribute:这样做以根据 name 属性查找您的输入:

$('input[name=sitebg]').val('000000');

jQuery way to change value on text input field is: jQuery 更改文本输入字段值的方法是:

$('#colorpickerField1').attr('value', '#000000')

this will change attribute value for the DOM element with ID #colorpickerField1 from #EEEEEE to #000000这会将 ID 为#colorpickerField1的 DOM 元素的属性value#EEEEEE#000000

最佳实践是直接使用标识:

$('#id').val('xxx');

设置元素的新值时,需要调用触发器更改。

$('element').val(newValue).trigger('change');

Just adding to Jason's answer, the .只是添加到杰森的答案中, . selector is only for classes.选择器仅适用于类。 If you want to select something other than the element, id, or class, you need to wrap it in square brackets.如果要选择元素、id 或类以外的内容,则需要将其括在方括号中。

eg例如

$('element[attr=val]')

jQuery change input text value using parent class jQuery 使用父类更改输入文本值

$('.form-submit input.submit').val('Johnny Bravo');
  • .form-submit is the parent class .form-submit是父类
  • Johnny Bravo is the text that you want to change Johnny Bravo是您要更改的文本

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

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