将JS变量放在我的模态HREF链接中

时间:2017-06-19 21:47:05

标签: javascript jquery

我在警告窗口中正确显示了JS变量。我试图将相同的值放在紧跟警报的模态的href链接中。

在主页面上,我有一个脚本,在警告框中显示值 - 它从此处获取clicked_id值:

<span onclick='executeChildSupport(this.id)'><a href='#' data-toggle='modal'><i class='fa fa-th' aria-hidden='true'></i></a></span>

这是捕获clicked_id并在警告框中显示的JS脚本

 function executeChildSupport(clicked_id) {
    $(this).tooltip('hide');
    event.currentTarget.innerHTML = "<i class='fa fa-th' aria-hidden='true' style='margin-left:10px'></i>";
    alert(clicked_id);
    $('#supportShare').modal('show');
    return false
  }

在警报之后,会显示一个模态,我正在尝试放置&#34; clicked_id&#34;在模式页面中显示的JS变量也存在于HALF字符串中,如下所示(其中&#34; document.write(clicked_id)&#34;)

<a href="../_support/tip.php?id=" target="_blank" class="btn btn-primary"><i class='fa fa-money fa-3x' aria-hidden='true' style="padding-bottom:3px"></i><br>LINK HERE <script>document.write(clicked_id);</script></a>

有什么建议吗?

这里是模态代码(仅限于身体区域 - 我相信这是请求所需的一切)

<div class="modal-body" style="padding: 40px;color:#fff">
<table border="0" style="width:100%">
<tr>
<td colspan="3" style="padding:8px 3px 3px 2px">
<a href="../_support/tip.php?id=" target="_blank" class="btn btn-primary" style="background-color:#171717;width:100%;border:none;padding:30px">
<i class='fa fa-money fa-3x' aria-hidden='true' style="padding-bottom:3px"></i><br>LINK <script>document.write(+clicked_id+);</script></a>
</td>
</tr>
</table>
</div>

这个方向怎么样?

<a href="javascript:document.write('../_support/tip.php?id='+clicked_id'); target="_blank">LINK</a>

1 个答案:

答案 0 :(得分:1)

如果我理解了您的查询,那么您可以采用两种方式, (以下所有代码都不是绝对的工作代码,这些代码用于理解,您可能希望根据需要更改它们)

不推荐的方式是使用全局变量,然后在链接中使用它 例如:

var current_id = null;
......
alert(clicked_id);
current_id = clicked_id;
....

然后做

<a href="../_support/tip.php?id=" target="_blank" class="btn btn-primary">
<i class='fa fa-money fa-3x' aria-hidden='true' style="padding-bottom:3px"></i>
<br>
LINK HERE 
<script>document.write(current_id);</script>
</a>

推荐方式是在提醒后访问和更改dom的值 例如:

......
alert(clicked_id);
$('#clicked_id').text(clicked_id);
$('#dynamic_link').attr('href', '../_support/tip.php?id='+clicked_id);
$('#supportShare').modal('show');
.....

或者您可以在模态加载后使用回调

......
alert(clicked_id);
$('#supportShare').modal('show', function(){
    $('#clicked_id').text(clicked_id);
    $('#dynamic_link').attr('href', '../_support/tip.php?id='+clicked_id);
});
.....

然后改变一下:

<a href="#" target="_blank" class="btn btn-primary" id="dynamic_link">
<i class='fa fa-money fa-3x' aria-hidden='true' style="padding-bottom:3px"></i>
<br>
LINK HERE 
<span id="clicked_id"></span>
</a>
相关问题