我需要在index.cshtml中运行一个javascript函数,以清理一些可能不需要的字符和附加空格的文本框。
该站点在.NET CORE MVC上运行。
按钮代码:
<button onclick="executeValidationTest()" class="btn btn-success ladda-button ladda-button-infinite" data-style="expand-right" type="submit"><i class="fa fa-search"></i> Recherche</button>
问题是...搜索开始时没有先运行我的清理脚本。 结果,我得到了一些默认搜索中不需要的字符。
我尝试了另一个按钮,但效果很好,但用户讨厌单击多个按钮。
我注意到有一个包含脚本的代码:
@Html.AddResource(PageResourceType.Javascript, @<script>SomeScriptAlreadyInTheFile</script<)
我试图将代码放在这里,但是它也不起作用。
我的脚本:
function executerTestDeValidation() {
console.log("Script call ok");
if (document.getElementById("ISBN").value != "") {
var string = document.getElementById("ISBN").value;
// OUTPUT
console.log("Before Clean: " + string);
// ___________________________________
// START CLEAN - Use string variable
// Clear all non numbers
string = string.replace(/\D/g, "");
console.log("After Clean: " + string);
// Check char total numbers
var n = string.length;
console.log("Lenght: " + n);
// Check if full string is multiple of 13 - rest zero
var rest = n / 13;
if (n % 13 != 0) {
// IF DIVISION BY ZERO = ERROR
// console.log("Lenght: " + n + " is NOK");
document.getElementById("txtBox").value = "ERROR";
} else {
// IF DIVISION BY ZERO = GOOD
// console.log("Lenght: " + n + " is Good (Divisible par 13 et reste 0)");
// Add spaces every 13 Chars
var chuncks = string.match(/.{1,13}/g);
var string = chuncks.join(" ");
// console.log("After space split: " + string);
document.getElementById("ISBN").value = string;
// Check for last digit verification number on each ISBN number
// ...
}
}
}
我应该将脚本调用放在哪里,以便在执行默认搜索之前在按下按钮时运行脚本?
最好的问候 瑞
答案 0 :(得分:0)
我用onKeyUp()解决了这个问题,但是用户ADyson使我朝着正确的方向寻求了真正的功能解决方案。