dpka7974 2012-07-03 00:17
浏览 28
已采纳

输入类型图像URL值通过jquery

I have a form with number of input type images. I need to replace the webpage location to url of the image when a user clicks any image.

form is like,

<form action='free.php' id='freeForm' method='post' autocomplete="off">
<?
for($j=1;$j<=5;$j++)
{
?>
<a href="<?=$url;?>" class="anchor-url"> <input type="image" src="<?=$img;?>" /></a>
 <?
 }
 ?>
 </form>

$img and $url values from database.

My jquery is like,

$("#freeForm").submit(function()
    {
            var Form = { };
            Form['inputFree'] = $("#inputFree").val();

                    Form['freeTOS'] = '1';



              $('.anchor-url').click(function(e){
             e.preventDefault();
              alert($(this).attr('href'));
           });


            $.post('processFree.php', Form, function(data)
            {
                    if(data == "Success")
                    {
                            $("#FreeErrors").html('').hide();
                            swapToPane('paneSuccess');
                            return;
                    }

                    swapToPane('paneFree');
                    $("#FreeErrors").html(data).show();
            });

            return false;
    });

Now I am getting 'http://au.yahoo.com' when i click each image. But I want to replace 'http://au.yahoo.com' with URL of each image. How can I pass the PHP variable $url to this form? Any idea?

Thanks!

  • 写回答

1条回答 默认 最新

  • douhang8991 2012-07-03 00:23
    关注

    This should work, if I understood what you were asking for correctly and though I haven't tried it. Basically you bind the click event to each image, and on click it replaces the window location with the value in the src attribute of the input element being clicked on.

    $('input[type=image]').click(function(e){
      e.preventDefault();
      window.location.replace($(this).attr('src'));
    });
    

    EDIT

    First you should add a class name to your anchor elements:

    <form action='free.php' id='freeForm' method='post' autocomplete="off">
    <?
    for($j=1;$j<=5;$j++)
    {
    ?>
    <a href="<?=$url;?>" class="anchor-url"> <input type="image" src="<?=$img;?>" /></a>
     <?
     }
     ?>
    </form>
    

    Your JS:

    $('.anchor-url').click(function(e){
      e.preventDefault();
      window.location.replace($(this).attr('href'));
    });
    

    Notice how I added a class to the anchor links. This can be named almost anything. This gives you the ability to select for those elements with that classname only with jQuery using the $('.any-classname') selector.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题