简体   繁体   English

如何使这个简单的Try / Catch测试工作?

[英]How do I get this simple Try/Catch test to work?

A user selects whether or not to run an alert line of code with an error using a radio button. 用户使用单选按钮选择是否运行带有错误的警报代码行。 The idea is the user selects a radio element, a function(setError()) is run that locates the selected radio button and then passes the value to another function (runCode()) that uses IF statements to decide whether or not to include errors in the alert. 想法是用户选择一个无线电元素,运行一个函数(setError())来定位所选的单选按钮,然后将该值传递给另一个函数(runCode()),该函数使用IF语句来决定是否包含错误在警报中。 If the user chooses to alert with an error then a Try Catch code should capture the error and display the error in the 如果用户选择以错误警告,则Try Catch代码应捕获错误并在中显示错误

element already in the HTML. 元素已经在HTML中。

<form name="Form">
       Run Code:
    <input type="radio" name="errorRad" onchange="setError()" value="yes">With Error
    <input type="radio" name="errorRad" onchange="setError()" value="no">Without Error
    </form>
        <p id="t">
        </p>

Script: 脚本:

<script>


function setError() {
    var x;
 var rad = document.forms["Form"]["errorRad"];
    for (i = 0; i < rad.length; i++) {
    if (rad[i].checked){
    x = rad[i].value;
    }
    }
   runCode(x);

    }
       function runCode(error) {
   if (error == "yes") {
    try {
    allert(a successful alert box);
    } catch(err) {
    document.getElementById("t").innerHTML = err.message;
    }
    }else if (error == "no") {
    alert("a successful alert box");
    }

    }

</script>

Interestingly, after testing this by placing random alert boxes in during the code to test where the code gets up to before breaking: the code doesn't even seem to call the first function (setError). 有趣的是,在测试之后通过在代码中放置随机警报框来测试代码在破坏之前到达的位置:代码甚至似乎都没有调用第一个函数(setError)。 The onchange event doesn't seem to work properly. onchange事件似乎无法正常工作。 Perhaps by syntax is wrong or an alternative event is better. 也许通过语法错误或替代事件更好。 Another point of interest is the calling of the second function (runCode). 另一个感兴趣的点是调用第二个函数(runCode)。 I'm not too good at passing parameters in JavaScript (I'm not good at much tbh) and I'm not sure if my method will work. 我不太擅长在JavaScript中传递参数(我不太擅长)并且我不确定我的方法是否有效。

JS Fiddle: http://jsfiddle.net/rwowf5j8/22/ JS小提琴: http//jsfiddle.net/rwowf5j8/22/

Try this 尝试这个

var rad = document.forms["Form"]["errorRad"];

for (var i = 0; i < rad.length; i++) {
    rad[i].addEventListener('change', onChange);
}

function onChange(event) {
    //cast as bool
    if (!!event.target.value) {
        try {
            allert('a successful alert box');
        } catch(err) {
            document.getElementById("t").innerHTML = err.message;
        }
    } else {
        alert('a successful text box');   
    }
}

there are a few issues with your code, the allert will throw an undefined is not a function error, but passing in a successful alert box not as a string stops the code from running 你的代码有一些问题,allert会抛出一个undefined不是函数错误,但是传递a successful alert box而不是字符串会阻止代码运行

fiddle - http://jsfiddle.net/rwowf5j8/24/ 小提琴 - http://jsfiddle.net/rwowf5j8/24/

I changed your code a little bit, and removed your error with allert ... 我改变你的代码一点点,并与阿勒特删除你的错误...

alert("a successful alert box");

http://jsfiddle.net/f3b5kxx3/ http://jsfiddle.net/f3b5kxx3/

But the rest was pretty fine. 但其余的都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM