如何选择一个按钮并将其值显示在屏幕上

时间:2019-04-09 17:51:53

标签: javascript html random getelementbyid id

我希望能够从按钮列表中进行选择,并将此按钮的值显示在屏幕上。我有一个函数,但不确定在HTML或函数中要包含哪些参数。

////////////////////////////////////////////fixed
module registermemory(
input [3:0]Ra, Rb, Wa, Wd,
input write, input clk,
output [3:0] A,B
);

reg [15:0] memarray [3:0]; 

integer i;
initial begin
	for(i=0;i<=15;i=i+1)
		memarray[i]<=4'b0101; ///when the entire 2D array has same value?
end
		
integer r1,r2,w1;
always@(clk)
begin
r1=Ra;r2=Rb;w1=Wa;
end

assign A = memarray[r1];
assign B = memarray[r2];//assign used outside always. equal to used inside always.
always@(Wd)
	begin	
		if(write)
			memarray[w1]=Wd;
		end
		
endmodule
///////////////////////////////
module controlblock(opcode,cntrl,Ra,Rb,Wa,write);
input [13:0]opcode;
output reg[1:0]cntrl;
output reg[3:0]Ra;
output reg[3:0]Rb;
output reg[3:0]Wa;
output reg write; //control signal tells that register has to be written in reg file


always@(opcode) ///why @opcode tho?
begin
cntrl=opcode[13:12];            
Ra=opcode[11:8];
Rb=opcode[7:4];
Wa=opcode[3:0];
write=1; //why tho?
end

endmodule
///////////////////////////////
module alu_arith(input[3:0]A, input[1:0] cntrl,
input clk,  
input[3:0]B,
output reg[3:0]Wd
);

always@(clk)
begin
case(cntrl)
	00:Wd=A+B;
	01:Wd=A-B;
	10:Wd=A*B;
	11:Wd=A/B;
default: Wd=4'b0000;
endcase
end
	
endmodule

///////////////////////////////////////////////////////
module concat(input [0:13]opcode, input clk, output[3:0]Wd);

wire[3:0]Ra;wire[3:0]Rb;wire[3:0]Wa;wire written;wire[1:0]cntrl;
wire[3:0]A;wire[3:0]B;wire[3:0]Wd;

controlblock a1(opcode,cntrl,Ra,Rb,Wa,write); //

registermemory a2(Ra, Rb, Wa, Wd,write,clk,A,B
); //

alu_arith a3(A,cntrl,clk,B,Wd); //
endmodule
//////////////////////////////////////////////////////////////
module testbench;
reg clk;
reg[13:0]opcode;
wire[3:0]Wd;
wire[1:0] cntrl;
reg[3:0]A,B;

concat a4(opcode,clk,Wd);

initial
clk=0;

always
#2 clk=!clk;
initial begin
	$display("\ttime \tclk  \tcntrl \tA  \tB  \tWd ");
    $monitor("%d,\t   \t%b   \t%b   \t%b   \t%b   \t%b",
    $time, clk, cntrl, A, B, Wd);
	
	#10 opcode=14'b00010010110100;
	#20 opcode=14'b01100010101010;
end

initial
#50 $finish;	

		
endmodule

1 个答案:

答案 0 :(得分:1)

最好避免使用onclick之类的内联事件,而使用addEventListener附加事件。

注意:firstbox变量必须已定义,并且必须引用要在其中打印文本的元素。

var buttons = document.getElementsByClassName("button");

for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener('click', click, false);
}

function click() {
  var firstbox = document.getElementById('firstbox');
  var buttonclicked = document.getElementById(this.id).value;

  firstbox.innerHTML = "Your Chosen button is: " + buttonclicked;
  this.disabled = true;
}
<input type="button" class="button" id="button2" value="2" onClick="click()">
<input type="button" class="button" id="button3" value="3" onClick="click(this)">
<input type="button" class="button" id="button4" value="4" onClick="click(this)">

<div id="firstbox"></div>