JS - 动态Href更改 [英] JS - Dynamic Href Change

查看:266
本文介绍了JS - 动态Href更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



不管代码如何:

 < script> 
var userinput = $('input [name =imdbid]');
$ b userinput.change(

function(){
$('#userimdb')。attr('href')+ $('input [name = imdbid]')。val();
}

);
< / script>

基本上,imdbid是从输入标记获得的,基本上我想追加任何用户类型一个标签的href,这似乎并没有工作。当我alert()时,它似乎给我输出它的意思,但是当我删除alert()它似乎并没有改变href,我也尝试过.setAttribute(),但那也没有做任何事情。 > 现在不要设置属性,只能访问它。另外,您需要将原始的href属性存储在某个地方,以避免重复添加相同的文本。这是更新后的代码:

  var userinput = $('input [name =imdbid]'); 
var orighref = $('#userimdb')。attr('href');
userinput.change(function(){
$('#userimdb')。attr('href',orighref + userinput.val());
});


Ive tried many way but im new to JS, I mainly do PHP.

Anyway the code:

    <script>
        var userinput = $('input[name="imdbid"]');

        userinput.change(

        function(){
            $('#userimdb').attr('href')+$('input[name="imdbid"]').val();
        }

        );
</script>

Basically, the imdbid is gotten from a input tag, basically I want to append whatever the user types to a href of a tag, This doesnt seem to work doe. When I alert() it, it seems to give me the output of what its meant to but when I remove alert() it doesnt seem to change the href, I also tried .setAttribute() but that also just did nothing.

Please help me out im going insane right now.

解决方案

You're not setting the attribute right now, only accessing it. In addition, you need to store the original href attribute somewhere so you're not adding the same text repeatedly. Here's the updated code:

var userinput = $('input[name="imdbid"]');
var orighref = $('#userimdb').attr('href');
userinput.change(function(){
    $('#userimdb').attr('href', orighref + userinput.val());
});

这篇关于JS - 动态Href更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆