简体   繁体   English

使用jQuery更改文本字段的值

[英]Changing value of text field with jQuery

I have a form input text field, and I want to change the text inside of the text field when the user clicks a button. 我有一个表单输入文本字段,并且我想在用户单击按钮时更改文本字段内的文本。 I can get the specific textfield with jQuery, but cannot change the text with .val(). 我可以使用jQuery获取特定的文本字段,但无法使用.val()更改文本。 Is there another way this can be done, or am I doing something wrong. 还有另一种方法可以完成此操作,还是我做错了什么。

Here is the text field: 这是文本字段:

<input type="text" name="textBox" id="Stage 1text" size="20" value="Enter Current Comp." align="center">

Then I use this to identify and change the text field. 然后,我用它来识别和更改文本字段。 where in this case stage = "Stage 1" and dance is the string I want to place in the text field. 在这种情况下,stage =“ Stage 1”,而dance是我要在文本字段中放置的字符串。

str = "#" + stage + 'text';
alert(str + '::' + dance); // confirm that strings are correct
jQuery(str).val(dance);

id attributes should not contain spaces. id属性不应包含空格。

You could change your code like so: 您可以这样更改代码:

<input type="text" name="textBox" id="Stage_1text" size="20" value="Enter Current Comp." align="center">

str = "#" + stage.replace(' ','_') + 'text';
alert(str + '::' + dance); // confirm that strings are correct
jQuery(str).val(dance);

An ID should not have spaces. ID不能包含空格。 Stage 1text is an invalid ID 阶段1文字是无效的ID

Add an underscore instead of space 添加下划线而不是空格

<input type="text" name="textBox" id="Stage_1text" size="20" value="Enter Current Comp." align="center">

and try below, 然后尝试

//           v--- assuming this will be Stage_1
str = "#" + stage + 'text';
alert(str + '::' + dance); // confirm that strings are correct
jQuery(str).val(dance);

incase if stage is returned from backend.. then simply replace space with _ 如果舞台是从后端返回的..只需用_替换空格

 str = "#" + stage.replace(/ /g,"_") + 'text';

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

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