test_events.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. require_once('../phpQuery/phpQuery.php');
  3. // phpQuery::$debug = true;
  4. $form = <<<EOF
  5. <form>
  6. <input name='input-example'>
  7. <input name='array[array-example]'>
  8. <textarea name='textarea-example'></textarea>
  9. <select name='select-example'>
  10. <option value='first'></option>
  11. </select>
  12. <input type='radio' name='radio-example' value='foo'>
  13. <input type='checkbox' name='checkbox-example' value='foo'>
  14. </form>
  15. EOF;
  16. $doc = phpQuery::newDocumentHTML($form);
  17. $inputs = $doc['form > *'];
  18. // creates array from input names
  19. // $results = $inputs->get(null,
  20. // create_function('$node', 'return $node->getAttribute("name");')
  21. // );
  22. $results = array();
  23. foreach($inputs as $node) {
  24. $node = pq($node);
  25. $name = $node->attr('name');
  26. $results[$name] = false;
  27. $node->change(
  28. new CallbackReference($results[$name])
  29. );
  30. }
  31. $inputs
  32. ->not('select,:checkbox,:radio')
  33. ->val('new value')
  34. ->end()
  35. ->filter('select')
  36. ->val('first')
  37. ->end()
  38. ->filter(':checkbox')
  39. ->val(array('foo'))
  40. ->end()
  41. ->filter(':radio')
  42. ->val(array('foo'))
  43. ->end()
  44. ;
  45. foreach($results as $name => $result) {
  46. print $result
  47. ? "Test for '$name' PASSED :)<br />\n"
  48. : "Test for '$name' <strong>FAILED</strong> !!!<br />\n";
  49. }