Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在页面加载方法中调用Javascript函数?_Javascript_Asp.net - Fatal编程技术网

如何在页面加载方法中调用Javascript函数?

如何在页面加载方法中调用Javascript函数?,javascript,asp.net,Javascript,Asp.net,我正在使用一个货币转换器Web服务,我已经编写了一个Javascript来显示点击按钮的结果。我希望javascript在页面加载时启动。我试过这么做 protected void Page_Load(object sender, EventArgs e) { ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Javascript", "function(data);", true); } 但该功能仍然无法启动

我正在使用一个货币转换器Web服务,我已经编写了一个Javascript来显示点击按钮的结果。我希望javascript在页面加载时启动。我试过这么做

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Javascript", "function(data);", true);
}
但该功能仍然无法启动。 这是我正在使用的JS

<script type="text/javascript">
    $(function () {
        $('#btnConvert').click(function () {
            var amount = $('#txtAmount').val();
            var from = $('#ddlfrom').val();
            var to = $('#ddlto').val();
            $.ajax({ type: "POST",
                url: "WebService.asmx/CurrencyConversion",
                data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var dot = data.d.indexOf(".");
                    var eg = data.d.substring(0, dot + 2);
                    var eg1 =data.d.substring(11, 50);
                                        $('#currency_converter_result').html(eg + eg1);
              }
            });
        });
    });
</script>

$(函数(){
$('#btnConvert')。单击(函数(){
var金额=$('#txtAmount').val();
var-from=$('#ddlfrom').val();
var to=$('#ddlto').val();
$.ajax({type:“POST”,
url:“WebService.asmx/CurrencyConversion”,
数据:“{金额:“+amount+”,从货币:“+from+”,到货币:“+to+”}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
var dot=数据d.indexOf(“.”);
var eg=数据d子串(0,点+2);
var eg1=数据d子串(11,50);
$('#currency_converter_result').html(eg+eg1);
}
});
});
});

它应该可以工作,但您正在发送JS
函数()这意味着什么都没有

$(函数(){})
只是
$(document.ready(function(){})的别名


您不需要在代码隐藏中执行任何操作

$(函数(){
});

相当于

$(document).ready(function(){})


因此,
$(function(){})中的任何内容

如果我理解您的问题,您希望在加载时触发click事件。您不需要页面加载部分。只需将
单击()
添加到绑定中即可

<script type="text/javascript">
    $(function () {
        $('#btnConvert').click(function () {
            var amount = $('#txtAmount').val();
            var from = $('#ddlfrom').val();
            var to = $('#ddlto').val();
            $.ajax({ type: "POST",
                url: "WebService.asmx/CurrencyConversion",
                data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var dot = data.d.indexOf(".");
                    var eg = data.d.substring(0, dot + 2);
                    var eg1 =data.d.substring(11, 50);
                                        $('#currency_converter_result').html(eg + eg1);
              }
            });
        }).click();
    });
</script>

$(函数(){
$('#btnConvert')。单击(函数(){
var金额=$('#txtAmount').val();
var-from=$('#ddlfrom').val();
var to=$('#ddlto').val();
$.ajax({type:“POST”,
url:“WebService.asmx/CurrencyConversion”,
数据:“{金额:“+amount+”,从货币:“+from+”,到货币:“+to+”}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
var dot=数据d.indexOf(“.”);
var eg=数据d子串(0,点+2);
var eg1=数据d子串(11,50);
$('#currency_converter_result').html(eg+eg1);
}
});
})。单击();
});
})),


您不需要使用任何来自代码隐藏的js调用

@Qantas94Heavy:是的,我说了。$(函数(数据){})我忘了将数据作为我正在使用的参数。我做了一个编辑!它不会改变任何东西,你不会调用任何函数。我是javascript新手,我不知道这有这么简单。非常感谢,先生。这是我在这个网站上学到的“技巧”之一。只是传授知识而已。
  $( document ).ready(function() {
 $('#btnConvert').click(function () {
// paste the other code