简体   繁体   English

根据jQuery中的元素“值”更改HREF链接

[英]Change HREF link depending on elements “value” in jquery

I have the following PHP that returns records from a my MYSQL table. 我有以下PHP,可从我的MYSQL表返回记录。 These records are displayed as LINKS. 这些记录显示为链接。 See code below... 参见下面的代码...

        <div class="slide1" id="u1026">

        <?php while ($row = mysql_fetch_array($query_rental)) {
        echo "<a class='fancybox fancybox.iframe' id='rental' value={$row['layout']} href=\"brochures\items-rental.php?id={$row['client_name']}\"></a>"; 
        }?>                 

        </div>

What I would like, is for the HREF link to change to 我想要的是将HREF链接更改为

\\"brochures\\items-rental-layout2.php?id={$row['client_name']}\\ \\“小册子\\项目租赁,layout2.php?ID = {$行[ 'CLIENT_NAME']} \\

If VALUE contains the text "layout2". 如果VALUE包含文本“ layout2”。 I know that I can change HREF using jquery code 我知道我可以使用jquery代码更改HREF

$(document).ready(function () {  
  $("#event").attr("href", "http://the.new.url")
});

I'm just not sure how to do that depending if the VALUE contains text "layout2". 我只是不确定如何执行此操作,具体取决于VALUE是否包含文本“ layout2”。 Any help is much appreciated. 任何帮助深表感谢。 Thanks 谢谢

You can just do it straight in the PHP code: 您可以直接在PHP代码中进行操作:

while ($row = mysql_fetch_array($query_rental)) {
  $layoutFlag = $row['layout'] == 'layout2' ? '-layout2' : '';
  echo "<a class='fancybox fancybox.iframe' id='rental' value=\"{$row['layout']}\" href=\"brochures\items-rental{$layoutFlag}.php?id={$row['client_name']}\"></a>"; 
}

You could also do it with Javascript: 您也可以使用Javascript来做到这一点:

$(function () {
  // I'm assuming you are going to turn it into a rental class, otherwise change the selector to whatever.
  $("a.rental").each(function() {
    var rentalItem = $(this);
    if (rentalItem.attr('value') === 'layout2') {
      // You can choose what to replace, as long as you know it will replace EXACTLY what you want it to. I'm just going with Regex's ^ (start-of-line) operator to make sure that what we are replacing is at the start of the line...
      rentalItem.attr('href', rentalItem.attr('href').replace(/^brochures\\items\-rental/, 'brochures\\items-rental-layout2'));
  });
});

As you can see, just doing it in PHP is so much easier. 如您所见,仅用PHP进行操作就容易得多。

Also as a side note, you are creating multiple elements with the same id . 另外请注意,您正在创建具有相同id多个元素。 Maybe you meant class='fancybox fancybox.iframe rental' ? 也许您是说class='fancybox fancybox.iframe rental'吗?

And as a second side note, I suggest using the data- prefix for holding custom data. 作为第二点,我建议使用data-前缀保存自定义数据。 In layout's case, use data-layout='layout-whatever' . 在布局的情况下,请使用data-layout='layout-whatever' You can then use .attr('data-layout') to get the layout attribute (it's easier to understand what that code is doing too!). 然后,您可以使用.attr('data-layout')获得layout属性(更容易理解该代码在做什么!)。

You can either run the IF statement on the PHP loop 您可以在PHP循环上运行IF语句

while ($row = mysql_fetch_array($query_rental)) {
    echo "<a class='fancybox fancybox.iframe' id='rental' value={$row['layout']} href=\"brochures\items-rental".($row['layout'] == 'layout2' ? '-layout2' : '').".php?id={$row['client_name']}\"></a>"; 
} 

Or by jQuery 或通过jQuery

$( "a.fancybox" ).each(function( index ) {
    if($(this).val() == "layout2") {
       oldHref = $(this).attr('href');
       newHref = oldHref.replace('items-rental.php', 'items-rental-layout2.php')
       $(this).attr('href', newHref);
    }
});
  • all your links have the same ID which can cause some issues when you wuold want to work with them with jQuery. 您所有的链接都具有相同的ID,当您想与jQuery一起使用时,这可能会导致一些问题。

  • If you have more a tags with the fancybox class, try adding a unique class to these tags and update the each loop 如果您在fancybox类中有更多标签,请尝试向这些标签添加唯一的类并更新每个循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM