使用href调用带参数的javascript函数 [英] using a href to call javascript function with parameter

查看:212
本文介绍了使用href调用带参数的javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从href调用javascript函数.该函数具有一个将由eval函数检索的参数.但是会发生一些错误.

I'm trying to call javascript function from a href . the function has a parameter which will be retrieved by the eval function . But some error occurs .

脚本:

function rate(id) {

            // do something
        }

一个将调用该函数的标签:

the a tag that will call the function:

<a href="javascript:rate(" + <%#Eval("ID")%> + ")" >rate</a>

我想念什么?

推荐答案

您不应该这样做,但是当前遇到的问题可能是引用/连接.

You shouldn't be doing it like this, but the issue you're currently up against is probably is your quoting/concatenating.

如果<%#Eval("ID")%>只是产生一个INT,这应该可以工作:

If <%#Eval("ID")%> simply produces an INT, this should work:

<a href="javascript:rate( <%#Eval("ID")%> )" >rate</a>

如果是字符串,

<a href="javascript:rate( '<%#Eval("ID")%>' )" >rate</a>

应该为您做到这一点,尽管您需要处理<%#Eval("ID")%>产生任何带有单引号的情况.

should do it for you, although you need to handle the case of <%#Eval("ID")%> producing anything with a single quote in it.

课程:

我说您不应该这样做,因为javascript伪协议(javascript:)已失效且不正确.在最坏的情况下,您应该使用返回false的onclick.理想情况下,您将以编程方式分配事件,并阻止事件对象的默认操作.

I say you shouldn't be doing it like this because the javascript pseudo protocol (javascript:) is defunct and improper. At worst you should be using an onclick which returns false. Ideally you'd be assigning the event programatically and preventing the event object's default action.

这篇关于使用href调用带参数的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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