首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery mailto/tel: onchange

Jquery mailto/tel: onchange
EN

Stack Overflow用户
提问于 2015-11-03 13:44:52
回答 2查看 201关注 0票数 1

我有一个下拉列表,它从一个前缘循环中获取数据。数据被放入数据属性中,我试图用这些属性填充两个图标。以下是我对此的看法:

代码语言:javascript
复制
<div class="row contact-full-container">
<div class="container">
    <div class="col-xs-12">

        <div class="panel panel-contact-full">
            <div class="panel-body">
                <div class="col-sm-5 form-group">
                    <h3>@Model.InformationText</h3>
                    <label class="sr-only" for="contact-select"></label>
                    <div class="form-select">
                        <select class="form-control" id="contact-select" title="Choose area" name="">
                            @foreach (var contact in Model.ContactContentArea.ContentItems<ContactBlock>())
                            {
                                <option data-phone="@contact.Phone" data-email="@contact.Email" data-image="@contact.Image">@contact.Name</option>
                            }
                        </select>
                        <i class="fa fa-caret-down"></i>
                    </div>
                </div>

                <div class="col-xs-8 col-xs-push-3 col-sm-4 col-sm-push-3">

                </div>
            </div>

            <div class="panel-footer">
                <div class="col-xs-5 col-sm-8 contact-icons">
                    <a href="tel:" id="phone-href"><i class="fa fa-lg fa-phone"></i></a>
                    <a href="mailto:" id="mail-href"><i class="fa fa-lg fa-envelope"></i></a>
                </div>
            </div>
        </div>
    </div>
</div>

以下是我的jquery:

代码语言:javascript
复制
 $('#contact-select').change(function () {
    var email = $(this).children('option:selected').data('email');
    var phone = $(this).children('option:selected').data('phone');
    $('#phone-href').setAttribute('href', 'tel:' + phone);
    $('#mail-href').setAttribute("href", "mailto:" + email);

    //  set phone-href to phone - mind phone:
    //  set email-href to email - mind email:
});

当我运行我的站点时,这两个图标的值只是简单的"tel:“和"mailto:",不管它是在加载还是在下拉列表中进行更改。有人能发现我的错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-03 16:05:28

上面的答案几乎是正确的。如果您使用data(),则不需要将数据放在电子邮件、电话等前面。

代码语言:javascript
复制
$('#contact-select').change(function () {
        var email = $(this).children('option:selected').data('email'); 
        var phone = $(this).children('option:selected').data('phone');

$('#phone-href').attr('href', 'tel:' + phone);
$('#mail-href').attr("href", "mailto:" + email);
        //  set phone-href to phone - mind phone:
        //  set email-href to email - mind email:
});
票数 0
EN

Stack Overflow用户

发布于 2015-11-03 15:16:53

尝尝这个

如果有用的话请告诉我。

设置属性就像获得属性一样。只有你使用第二个值。就像$(this).attr('test', 'Data i want to put in test')

代码语言:javascript
复制
$('#contact-select').change(function () {
        //Don;t know about these 2. never used it like this.
        var email = $(this).children('option:selected').data('data-email'); //<==== you forgot the data-email
        var phone = $(this).children('option:selected').data('data-phone');//<==== you forgot the data-phone

$('#phone-href').attr('href', 'tel:' + phone);
$('#mail-href').attr("href", "mailto:" + email);
        //  set phone-href to phone - mind phone:
        //  set email-href to email - mind email:
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33500703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档