【问题标题】:Based on selection, "mailto:" link changes to "mailto: variable" using javascript根据选择,“mailto:”链接使用 javascript 更改为“mailto:变量”
【发布时间】:2014-08-17 19:16:36
【问题描述】:

我正在尝试制作一个表格。它的工作方式是用户从下拉列表中选择一个选项。然后他们单击确认位置按钮。最后他们点击发送电子邮件链接。基本上,当他们单击确认位置按钮时,它会根据他们在下拉列表中的选择更改发送电子邮件链接中的 mailto 链接。 但是,我想不出如何将 mailto 链接更改为 mailto 我的变量 t,该变量保存电子邮件地址的值(基于选择。) (见代码中的第三行) 请帮忙!

<form>Select your place:
    <select id="mySelect">
        <option value="email@domain.com">Location 1</option>
        <option value="email2@domain.com">Location 2</option>
        <option value="email3@domain.com">Location 3</option>
        <option value="email4@domain.com">Location 4</option>
    </select>
</form>
<button type="button" onclick="displayResult()">Confirm Location</button>
<a id="myEmailList" href="mailto:">Send email</a>
<script type="text/javascript">
function displayResult() {
    var x = document.getElementById("mySelect");
    t = x.value
    document.getElementById("myEmailList").href = "mailto:"t;
}
</script>

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    嗯,连接值?

    document.getElementById("myEmailList").href = "mailto:"+t;
    

    【讨论】:

    • 非常感谢!这只是我使用 JavaScript 的第二天,因此很困难。我请求你看看我的另一个问题,它现在真的很困扰我。谢谢! :stackoverflow.com/questions/25344199/…
    【解决方案2】:

    选择元素本身没有值;您需要获取所选选项的值。试试这个:

    t = x.options[x.selectedIndex].value;
    

    在设置 href 时,您也缺少一个 +。应该是:

    href = "mailto:" + t
    

    【讨论】:

    • + 起作用了。至于第一个建议,它对我有用吗? :S
    猜你喜欢
    • 2011-04-21
    • 2018-10-13
    • 2019-02-05
    • 1970-01-01
    • 2023-03-19
    • 2012-10-25
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多