开发者

How to send some data from dynamic form using jquery?

I am trying to send data from dynamicly generated cheboxes(when ticked) to my php script, and then to mysql data base. I've got something like that:


form method="post" id="form">
<input type="checkbox" id="1" name="tick">
<input type="checkbox" id="2" name="tick">
<input type="checkbox" id="3" name="tick">
<input type="checkbox" id="4" name="tick">
<input type="checkbox" id="5" name="tick">
<input type="checkbox" id="6" name="tick">
</form>

(amount of checkboxes is not const!)


$(document).ready(function(){
 $("form#form").check(function() {
  var achieved = $('input[check]');
  var id = $('input[id]');

  var data = 'achieved=' + achiev开发者_如何转开发ed.val() + '&amp;id=' + id.val();

  $.ajax({
   type: "POST",
   url: "change.php",
   data: data,
   success: function(){
    $('div.changed').fadeIn(400);
    setTimeout(function(){ $("div.changed").fadeOut(400) }, 4000);
   }

  });
  return false;
 });
});

What did I wrong?


You need to give your checkbox elements values (value="something") you can check for on the server side.

You can also use the serialize() method to create the parameters string instead of doing it manually.

The way you are doing it now is sure to not work because your achieved variable will not match any of your elements (did you mean :checkbox?) and id will match all, so the val() will only return the first value, which is not set in your example.

Also, don't use &amp; when you mean &. The encoded entity only makes sense in HTML.

I'm also sure there is no check() method in jQuery. Are you using a plugin ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜