having trouble in getting value of input box that have user defined index in jquery

StackOverflow https://stackoverflow.com/questions/21703396

  •  09-10-2022
  •  | 
  •  

Frage

<input id="start[2]" type="input" value="10" name="start[2]">

I know the index 2 and I am getting value of this input box using jquery, like this:

str="#start[2]";
result = $(str).val();
alert(result);

but it alerts "undefined".

War es hilfreich?

Lösung

From jQuery docs:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\

So you need to do:

str="#start\\[2\\]";
result = $(str).val();
alert(result);

Fiddle Demo

Andere Tipps

You need to escape [ and ] using \

but \ is also special character so you have to escape it also \\

result = $("#start\\[2\\]").val();

Fiddle Demo

you can refer the given fiddle link for this

 <input id="start[2]" type="input" value="10" name="start[2]">

http://jsfiddle.net/GA63Y/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top