简体   繁体   English

如何在 javascript 中将文本输入变量添加到 URL

[英]How to add text input variable to URL in javascript

I'm having the weirdest problem right now.我现在遇到了最奇怪的问题。 I have this JS code:我有这个JS代码:

    function CreateChat() {
    var chatName = document.getElementById("chatName").value;
        window.location.href = "https://localhost:44321/CreateNewChat/CreateChat?ChatName=" + chatName;
}

Thing is, as it is, I can't make it work.事情是这样的,我无法让它工作。 It obviously should be calling a controller method, but it just won't work.它显然应该调用 controller 方法,但它不起作用。 If I hit that url with the project running and put anything at the end of it, like https://localhost:44321/CreateNewChat/CreateChat?ChatName=TestName , that test name variable will get to the controller without a problem. If I hit that url with the project running and put anything at the end of it, like https://localhost:44321/CreateNewChat/CreateChat?ChatName=TestName , that test name variable will get to the controller without a problem. If I hardcode to the code the "TestName" instead of passing the chatName variable I define earlier, it will get to the controller, no problem.如果我将“TestName”硬编码到代码中,而不是传递我之前定义的 chatName 变量,它将到达 controller,没问题。 Hell, if I debug the script, the chatName variable gets loaded correctly with my input, and if I console.log the url it will show up correctly (in fact, I can copy/paste that url and it will hit the controller method correctly). Hell, if I debug the script, the chatName variable gets loaded correctly with my input, and if I console.log the url it will show up correctly (in fact, I can copy/paste that url and it will hit the controller method correctly )。 But, as the code is presented above, it will never, by any means, hit the controller.但是,正如上面提供的代码,它无论如何都不会命中 controller。 It will reach that point, and cut the execution as if there was an error in the JS code.它将到达那个点,并切断执行,就好像 JS 代码中有错误一样。 Do you guys have any ideas on this?你们对此有什么想法吗? It's driving me mad, really.这让我发疯,真的。

Just in case, this is how I define the text input in the HTML:以防万一,这就是我在 HTML 中定义文本输入的方式:

<input type="text" required class="form-control" id="chatName" aria-describedby="nameHelp" placeholder="Name your chat!">

Edit: encodeURIComponent on the chatName doesn't work either.编辑: chatName 上的 encodeURIComponent 也不起作用。

try using window.open with _self instead:尝试将window.open_self一起使用:

window.open ('https://localhost:44321/yadayada','_self',false);

You have a submit button (and I assume this button is inside a form )... when you click on the submit button the form is submitted to the submit action of the form, this is the default behavior for the submit action.您有一个提交按钮(我假设此按钮在form内)...当您单击提交按钮时,表单被提交到表单的提交操作,这是提交操作的默认行为。

Now if you don't want your page to be submitted, you can change the button type to button :现在,如果您不想提交页面,可以将按钮类型更改为button

<button type="button" onclick="CreateChat()" class="btn btn-primary">Create a New Chat!</button>

Alternatively if you actually need a submit button then you need to prevent the default behavior in order to change window.location :或者,如果您确实需要submit按钮,那么您需要阻止默认行为以更改window.location

function CreateChat(event) {
    e.preventDefault(); // <-- don't submit the form
    var chatName = document.getElementById("chatName").value;
        window.location.href = "https://localhost:44321/CreateNewChat/CreateChat?ChatName=" + chatName;
}

You need to pass the event to CreateChat function:您需要将event传递给CreateChat function:

<button type="submit" onclick="CreateChat(event)" class="btn btn-primary">Create a New Chat!</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM