开发者

Post elements that is not in the form tag?

I have code like so:

<form enctype="multipart/form-data" method="POST" action="blabla.php" class="addToCart">
  <input type="button" onclick="addToCartMulti();" value="Add Tracks to Cart" id="addToCart" name="开发者_高级运维addToCart">
</form>

Outside of the form tags, I have tracklisting:

<ul>
 <li><input type="checkbox" checked onclick="onSongClick(this);" id="trackNr_1" name="trackSelect" class="trackSelectionCheck"> 1. Train Love</li><li>
  .....
</ul>

I want to be able to post the form and include these other values. I have something like this for the JavaScript so far:

function addToCartMulti() {
  $("input.trackSelectionCheck").each(function(index) {
              var track = $(this).attr("id");
              var start = 8;
              var end = track.length;
              track = track.substring(start,end);
              $("form.addToCart").submit();
        });
  }

So the problem is that somewhere before this: $("form.addToCart").submit();. I need to add some JavaScript to actually include the other checkboxes so they are posted to the page.

Note: I don't want to use ajax.


Add a hidden form field to your form, and then copy that track data you need, in whatever format you want (eg, a comma delimited list), into that hidden field. Then call submit just as you are doing now.

Note: It appears you currently calling sumbit inside of the "each" loop, which is probably not what you want. You want to process all the checkboxes with each, and then after that is done, do the submit. So the "submit()" call should be outside of that each loop, unless I am misreading your intention.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜