开发者

jquery submit form from text link

开发者 https://www.devze.com 2022-12-16 13:19 出处:网络
I\'m trying to create a small form that submits a form and passes a value from a text link.. Here\'s what I\'ve got so far..

I'm trying to create a small form that submits a form and passes a value from a text link.. Here's what I've got so far..

<form id="calendar" name="calendar" action="" method="post">
<a href开发者_如何学Go="<?php echo $next_month; ?>" class="submitCal">&raquo;</a>
</form>

<script type="text/javascript">
jQuery.noConflict();
jQuery('.submitCal').click(function () {
  jQuery("#calendar").submit();  
});
</script>

I'd like the href to become a form variable called "time", I'm just not sure if I'm heading in the right direction or not.. If someone could help, that'd be great :)

Cheers


You could do it like this:

jQuery('.submitCal').click(function () {
  var timeVar = jQuery("<input type='hidden' name='time'/>");
  timeVar.val(jQuery(this).attr("href"));
  jQuery("#calendar").append(timeVar).submit();  
  return false;
});

This will create a hidden form element inside your form, with the value of your href attribute


Probably the easiest thing to do would be just output a hidden form field with the time value there as well. That makes your form compatible with no-javascript browsers too.

<form id="calendar" name="calendar" action="" method="post">
<input type="hidden" value="<?php echo $next_month; ?>" name="time" />
<a href="<?php echo $next_month; ?>" class="submitCal">&raquo;</a>
</form>

Then you're done and done! Your jquery looks good except for the noConflict() bit. The best practice for that is to stick it in your document header, right after you've loaded the jQuery libraries.


If you're using jQuery 1.4 this might work:

jQuery('.submitCal').click(function() {
    $(this).closest('form').append($('<input>', {
        id: 'time',
        name: 'time',
        val: $(this).attr('href')
    })).submit();
});
0

精彩评论

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

关注公众号