使用onclick更改函数内的变量



如果单击单选按钮,我的javascript需要更改函数内的变量。我已经尝试了几种方法- if else语句是其中之一,但到目前为止没有任何工作。

在我的最后一次尝试中,我试图在click上执行一个函数,它将重置我在第一个函数中声明的变量。

代码的全部目的是生成一个随机密码,并可选择使用特殊符号

它也意味着是一个Chrome扩展,所以使用内联代码是禁止的。

<html>
<br>
<h1>Set the number of characters</h1>
<input id="num"  type = "number" value = "20" name = "value"><br><br>

<form>
<input type="radio" type = "text"  id="spec_symb" name="symbols" value="specialSymbols">
<label for="spec_symb">Use special symbols</label><br>
</form>
<button id="click">Generate</button><br>
<p id="text" ></p>

<script src="jquery-3.5.1.min.js"></script>
<script src="pass.js"></script>

</html>
function pass(length) {
var passw = "";
var symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
document.getElementById("spec_symb").onclick = function(){func()};
function func(){window.symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_"
};
var slength = symbols.length;
for ( var i = 0; i < length; i++ ) {
passw = passw + symbols.charAt(Math.floor(Math.random() * slength));
}
return passw;}
document.getElementById("click").onclick = function(){myFunction()};
function myFunction(){
document.getElementById("text").innerHTML = passw(document.getElementById("num").value);
}

使用document.getElementById('spec_symb').checked语句使用if/else语句

那应该能让你到达你想去的方向。

每次你调用pass()函数时,你将symbols变量赋值为ABC..,你期望用户在调用函数的同时点击#spec_symb(这几乎是不可能的)。而不是这样做,你应该验证输入是否被检查,然后分配一个新值,在你的情况下,你可以只是添加到旧字符串,像下面的例子。

document.querySelector("#pass").onclick = () => {
let chars = "ABC...";
if (document.querySelector("#input").checked)
chars += "#@$";
console.log(chars);
};
<input id="input" type="checkbox" />
<button id="pass">Result</button>

我已经把单选改为复选框,所以你可以取消标记。

您的结构有点偏离,您正在尝试调用不存在的passw函数。

您也不应该在pass函数中设置symbols。首先将其设置为默认值,如果复选框被选中,则可能重写它。

你可能还应该使用复选框而不是单选按钮,因为一旦单选按钮被选中,就没有办法取消选中。

我想这就是你想要的:

// Set reference to DOM elements just once, not in the functions
const input = document.getElementById("num");
const output = document.getElementById("text");
const special = document.getElementById("spec_symb");
special.addEventListener("click", func);
document.getElementById("click").addEventListener("click", myFunction);
// Set the symbols to the default set of characters
let symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

function func(){
// Override the symbols if the checkbox was checked.
if(special.checked){
symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_";
} else {
symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
}
}
function myFunction(){
output.textContent = pass(input.value);
}
function pass(length) {
var passw = "";

var slength = symbols.length;
for ( var i = 0; i < length; i++ ) {
passw = passw + symbols.charAt(Math.floor(Math.random() * slength));
}
return passw;
}
<h1>Set the number of characters</h1>
<input id="num"  type = "number" value = "20" name = "value"><br><br>

<form>
<input type="checkbox" id="spec_symb" name="symbols" value="specialSymbols">
<label for="spec_symb">Use special symbols</label><br>
</form>
<button id="click">Generate</button>
<p id="text" ></p>

最新更新