我正在开发一个小项目,我的任务是创建一个生成随机字符串的按钮,然后使用另一个按钮将其作为查询字符串附加到URL。我已经完成了随机字符串生成器,但是我在创建一个按钮以将其附加到URL时遇到了困难。我想知道实现这一目标的最佳方法是什么。
这是我到目前为止所拥有的:
<!DOCTYPE html>
<html>
<body>
<h1>HTML PROJECT</h1>
<script lauguage="javascript" type="text/javascript">
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var string_length = 10;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
document.getElementById("randomfield").value = randomstring;
}
</script>
<fieldset>
<h1>Generate String!</h1>
<form name="randform" >
<input type="button" value="Generate" onClick="randomString();">
<input type="text" name="randomfield" id="randomfield" value="" disabled>
</form>
</fieldset>
<script>
var url = file:///I:/completed!.htm? + text;
</script>
<fieldset>
<h1>Now press the button below to place the string on the URL!</h1>
<form action= file:///I:/completed!.htm" + randomString()' >
<button type="submit">String Me!</button>
</form>
</fieldset>
</body>
</html>
答案 0 :(得分:1)
<强> HTML 强>
<h1>HTML PROJECT</h1>
<fieldset>
<h1>Generate String!</h1>
<form name="randform" >
<input type="button" value="Generate" id = "generate"/>
<input type="text" name="randomfield" id="randomfield" value="" disabled ></input>
</form>
</fieldset>
<fieldset>
<h1>Now press the button below to place the string on the URL!</h1>
<form name="myform">
<button type="submit">String Me!</button>
</form>
</fieldset>
<强> JS 强>
var url;
var btn = document.getElementById('generate');
generate.addEventListener('click', function(){
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var string_length = 10;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
document.getElementById("randomfield").value = randomstring;
url = 'file:///I:/completed!.htm?'+randomstring;
document.myform.action = url;
});
document.myform.addEventListener('submit', function(){
alert(this.action);
});
答案 1 :(得分:0)
根据您的需要使用window.location或get ajax请求。
答案 2 :(得分:-2)
$(".searchButton").click(function(){
var textVal = $("input:text[name=InputBox]").val();
value = "t=" + textVal;
location.href = "yourURL" + value;
});