开发者

How to send a form after a click

开发者 https://www.devze.com 2023-03-02 10:47 出处:网络
I have this code : <script type=\"text/javascript\"> $(document).ready(function() { $(\'a[name=linkArtist]\').click(function(e) {

I have this code :

<script type="text/javascript">
    $(document).ready(function() {
        $('a[name=linkArtist]').click(function(e) {
            e.preventDefault();

            $('#changeArtist').val($(this).att开发者_如何学编程r('title'));
        }); 
    });
</script>

<form action="index.php?explore=browse" method="post">
    <input type="hidden" id="changeArtist" name="artist" value="<?=$artist?>" />
    <a name="linkArtist" title="a" href="#">A</a> -
    <a name="linkArtist" title="b" href="#">B</a> - 
    <a name="linkArtist" title="c" href="#">C</a>
</form>

When I click on the button, I set the link value (as 'title') to the hidden field, and after I'd like to send that form! How can I do it? And what do you think about this code? Can I better it?


JavaScript:

<script type="text/javascript">
    $(document).ready(function() {
        $('a[name=linkArtist]').click(function(e) {
            e.preventDefault();

            $('#changeArtist').val($(this).attr('title'));
            $('#myForm').submit();
        }); 
    });
</script>

The form:

<form action="index.php?explore=browse" method="post" id="myForm">

Note the form id and the change to the click handler to access it.


I don't think you need to use preventDefault() at all. The form should not submit until the function has returned.

0

精彩评论

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