[英]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.