如何调用javascript函数?
我有一个java脚本函数,它通过按质量取价格*来帮助计算总成本如何调用javascript函数?,javascript,asp.net,vb.net,gridview,Javascript,Asp.net,Vb.net,Gridview,我有一个java脚本函数,它通过按质量取价格*来帮助计算总成本 <script type="text/javascript"> $("[id*=txtQuality]").live("change", function () { if (isNaN(parseInt($(this).val()))) { $(this).val('0');
<script type="text/javascript">
$("[id*=txtQuality]").live("change", function () {
if (isNaN(parseInt($(this).val()))) {
$(this).val('0');
} else {
$(this).val(parseInt($(this).val()).toString());
}
});
$("[id*=txtQuality]").live("keyup", function () {
if (!jQuery.trim($(this).val()) == '') {
if (!isNaN(parseFloat($(this).val()))) {
var row = $(this).closest("table");
$("[id*=lblTotal]", row).html(parseFloat($("[id*=price]", row).html()) * parseFloat($(this).val()));
}
} else {
$(this).val('');
}
});
</script>
$
只需在页面加载时触发事件,该事件将执行所需的代码并设置标签
$(document).ready(function(){
$("[id*=txtQuality]").live("change", function () {
if (isNaN(parseInt($(this).val()))) {
$(this).val('0');
} else {
$(this).val(parseInt($(this).val()).toString());
}
}).trigger("change");
$("[id*=txtQuality]").live("keyup", function () {
if (!jQuery.trim($(this).val()) == '') {
if (!isNaN(parseFloat($(this).val()))) {
var row = $(this).closest("table");
$("[id*=lblTotal]", row).html(parseFloat($("[id*=price]", row).html()) * parseFloat($(this).val()));
}
} else {
$(this).val('');
}
}).trigger("keyup");
});
您可以从onload处理程序中删除事件,或者从文档中删除事件。ready处理程序:
$(document).ready(function() {
$("[id*=txtQuality]").trigger("change")
.trigger("keyup");
});
或者,如果在load或document.ready中完成了更改和keyup绑定,您只需链接.trigger()
调用:
$(document).ready(function() {
$("[id*=txtQuality]").live("change", function() {
// your function body here
}).trigger("change");
// and the same for keyup
});
或者,您可以将代码更改为不将匿名函数用于事件处理程序,以便可以直接从其他位置调用该函数:
// declare function to be called on change
function myChangeHandler() {
// your existing change handler code here
}
// bind event to function
$(("[id*=txtQuality]").live("change", myChangeHandler);
// then you can call the function from other places, including onload:
myChangeHandler();
// and the same for your keyup
但是这个javascript在aspx中,如何在页面加载中使用?我把触发器放在它不工作的地方,把它放在
$(document.ready(function(){})
中。检查我编辑的答案。
$(document).ready(function() {
$("[id*=txtQuality]").live("change", function() {
// your function body here
}).trigger("change");
// and the same for keyup
});
// declare function to be called on change
function myChangeHandler() {
// your existing change handler code here
}
// bind event to function
$(("[id*=txtQuality]").live("change", myChangeHandler);
// then you can call the function from other places, including onload:
myChangeHandler();
// and the same for your keyup