Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - 将html表中的javascript变量传递给javascript函数(Passing a javascript variable in an html table to a javascript function)

I am trying to pass the variable fn to the function myFunc using a button.(我试图使用按钮将变量fn传递给函数myFunc。)

I am new to javascript is there anyway to do this.(我是javascript新手,无论如何都可以做到这一点。) I am using a table with the first and last name inputed by the use.The response should be a greeting.(我正在使用带有用户输入的名字和姓氏的表。响应应该是问候。) This table will eventually contain more rows and is part of a bigger project.(该表最终将包含更多行,并且是更大项目的一部分。) <html> <head> <script src="http://code.jquery.com/jquery-1.8.2.js"></script> <script language="JavaScript" type="text/javascript"> function ajax_post(){ var fn = document.getElementById("first_name").value; var ln = document.getElementById("last_name").value; var table = $("#resulttable"); table.append('<tr><th>First Name</th><th>Last Name</th><th>ADD</th></tr>'); table.append('<tr><td>'+ fn+'</td><td>'+ ln+'</td><td><button onClick="myFunc(fn)">Add</button></td></tr>') } function myFunc(fname){ var firstName= fname document.getElementById("status").innerHTML = "Hello" + firstName; } </script> </head> <body> <h2>Example</h2> Your First Name: <input id="first_name" name="first_name" type="text" /> <br /><br /> Your Last Name: <input id="last_name" name="last_name" type="text" /> <br /><br /> <input name="myBtn" type="submit" value="Submit Data" onClick="ajax_post();"> <br /><br /> <div id="status"></div> <br /><br /> <table border = "1" id="resulttable"> </body> </html>   ask by user2687915 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...