【问题标题】:JQuery - Display a hyperlinked textJQuery - 显示超链接文本
【发布时间】:2015-11-11 18:22:03
【问题描述】:

从用户选择的选项中寻找显示超链接的帮助。我正在尝试做的是根据复选框选择形成一个带有链接的句子。

所以应该是这样的……

您已表示对写作支持停车住房感兴趣。 - 这些关键字应该是链接。

我研究并提取了一些代码并让它显示href,但不确定如何从选项中动态创建链接。感谢任何帮助。

<input type="checkbox" name="resource" value="Housing" href="http://housing.fake.com"> Housing</input>
<input type="checkbox" name="resource" value="Parking" href="https://Parking.fake.com"> Parking</input>
<input type="checkbox" name="resource" value="Writing Support" href="https://writing.fake.com"> Writing Support</input>


<div id="result"></div>

<input type="submit" id="submit" name="submit" value="Show Me the Resources">

var $result = $('#result');

$('input[type="submit"]').click(function () {
    var total = $('input[type="checkbox"]:checked').length;
    if (total <= 0 ) {
        $('#result').text('You have not indicated any interest. Please choose some options.');
    }
    else if (total == 1) {
        $('#result').text('You have indicated an interest in ' + $('input[type="checkbox"]:checked').attr('href') + '. Please click on the link to learn more about these resources.');
    } else {
        $('#result').text('You have indicated an interest in ');
        $('input[type="checkbox"]:checked').each(function(i) {
            if(i == total-1) {          
                $result.append('and ' + $(this).attr('href') + '.<br/> Please click on the links to learn more about these resources.');
            } else {
                $result.append($(this).attr('href') + ', ');
            }
        });  
    }
});

JFFiddle

【问题讨论】:

    标签: jquery jquery-ui jquery-plugins


    【解决方案1】:

    试试这样:

    $('#result').html('You have indicated an interest in ' + '<a href="' 
    + $('input[type="checkbox"]:checked').attr('href') + '">'
    + $('input[type="checkbox"]:checked').attr('href') + '</a> '
    + '. Please click on the link to learn more about these resources.');
    

    【讨论】:

    • 我已经搞了一天了,你一分钟就解决了!我很欣赏这两个答案。他们最终解决了我的问题。非常感谢!
    • 不客气 :) 如果您需要帮助,请不要犹豫 ;)
    猜你喜欢
    • 2020-04-20
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多