2012-09-19 6 views
-1

양식 입력 텍스트 필드가 있고 사용자가 버튼을 클릭 할 때 텍스트 필드 내부의 텍스트를 변경하고 싶습니다. jQuery를 사용하여 특정 텍스트 필드를 가져올 수 있지만 .val()을 사용하여 텍스트를 변경할 수는 없습니다. 이것이 다른 방법으로 할 수 있습니까, 아니면 제가 잘못하고있는 것입니다. jQuery를 사용하여 텍스트 필드 값 변경

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

ID에 공백이 없어야합니다. 'Stage 1text'는 유효하지 않은 ID입니다 –

+0

감사합니다. ID에 공백이있을 수 없다는 것을 몰랐습니다. –

답변

1

. 무대 1text 무대가 백엔드에서 반환되는 경우 넣다

//   v--- assuming this will be Stage_1 
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(/ /g,"_") + 'text'; 
1

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); 
관련 문제