繁体   English   中英

使用jQuery更改文本字段的值

[英]Changing value of text field with jQuery

我有一个表单输入文本字段,并且我想在用户单击按钮时更改文本字段内的文本。 我可以使用jQuery获取特定的文本字段,但无法使用.val()更改文本。 还有另一种方法可以完成此操作,还是我做错了什么。

这是文本字段:

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

然后,我用它来识别和更改文本字段。 在这种情况下,stage =“ Stage 1”,而dance是我要在文本字段中放置的字符串。

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

id属性不应包含空格。

您可以这样更改代码:

<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);

ID不能包含空格。 阶段1文字是无效的ID

添加下划线而不是空格

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

然后尝试

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

如果舞台是从后端返回的..只需用_替换空格

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

暂无
暂无

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

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