0

我正在做一个小项目,我的任务是创建一个按钮,该按钮将生成一个随机字符串,然后使用另一个按钮将其作为查询字符串附加到 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>
4

3 回答 3

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);
});

工作示例

于 2013-10-10T20:46:42.737 回答
0

根据您的需要,使用 window.location 或获取 ajax 请求。

于 2013-10-10T20:28:11.280 回答
-2
$(".searchButton").click(function(){
var textVal = $("input:text[name=InputBox]").val();

value = "t=" + textVal;
location.href = "yourURL" + value;
});
于 2013-10-10T20:27:24.703 回答