javascript - Iterating through mutiple checkbox values and matching them against a single text input box's value with jQuery -


i trying check if value string of text input field contains matches correspond values of multiple checkbox inputs. if checkbox's value found match within text input's value string, checkbox should checked, while unmatching checkbox should remain unchecked. code below, of checkboxes show checked, while 1 of checkbox's value match text input's value string.

jquery

$("input[type='checkbox'][name='hello']").each(function(){     var value = $(this).attr('value');     var id = $(this).attr('id');       if ($("input[type='text'][name='goodbye']:contains("+value+")")) {         $("input[id="+id+"]").prop('checked', true);     } }); 

html

<input type="checkbox" name="hello" id="1" value="1"><label for="1">one</label> <input type="checkbox" name="hello" id="2" value="2"><label for="2">two</label> <input type="checkbox" name="hello" id="3" value="3"><label for="3">three</label>  <input type="text" name="goodbye" id="goodbye" value="1"> 

if want text compare following should work

$("input[type='checkbox'][name='hello']").each(function () {     var value = $(this).val();      if ($('#goodbye').val().indexof(value) >= 0) {         $(this).prop('checked', true);     } }); 

i'm not sure why going overly complicated goodbye input, since had id , id unique (or you'll have other issues).

$('#goodbye').val() 

will give value in input box. using indexof return -1 if value not found, number 0 or greater if is, can use figure out if value exists.

example: http://jsfiddle.net/x74nk/1/


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -