如何使用 JavaScript 或 Jquery 动态更改 HTML 文本输入

发布于 2025-01-13 22:12:55 字数 423 浏览 2 评论 0原文

我有一个返回字符串的网络服务。 https://localhost:5001/mywebservice

现在,我制作了一个带有文本输入的 HTML 页面。 我想使用 JavaScript 或 JQuery 来调用我的网络服务。 然后用我的网络服务返回的任何内容更新文本输入 https://localhost:5001/mywebservice

我尝试使用以下 JQuery 代码片段。 但这行不通。

$('button').on('click', function (e) {
    var str = $('https://localhost:5001/mywebservice').val();
    $("#MyString").prop('value', str);
});

如果有人能给我提示,我将不胜感激。 感谢您的帮助。

I have a web service that returns a string.
https://localhost:5001/mywebservice

Now, I made a HTML page with a text input.
I'd like to use to JavaScript or JQuery to call my webservice.
Then update the text input with whatever returns from my web service
https://localhost:5001/mywebservice

I tried to use the following JQuery code snippet.
But it won't work.

$('button').on('click', function (e) {
    var str = $('https://localhost:5001/mywebservice').val();
    $("#MyString").prop('value', str);
});

I'd appreciate it if someone can give me a hint.
Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2025-01-20 22:12:55

好的,首先,您正确地完成了事件侦听器。
如果此脚本在 HTML 文件中运行,则您可以从文档中选择元素。 。

$("<select your input>").val() 

如果它是文本框,则使用;如果它是元素,则使用 .html()
要请求您的服务的返回信息,请勿使用 jquery $。它不是为 ajax 构建的。

  1. 确保在使用平台时它始终启动并运行
  2. 而不是将其托管在本地主机上,而是尝试在该 js 所在的任何平台上运行它。
  3. 让本地主机仅显示您发送回的信息
  4. 发送 ajax 调用到您的本地主机网站,使用
$.ajax({
url:"https://localhost:5001/mywebservice",
success:function(d){
//d is your data
}
})

ok, so first of all, you did the event listener right.
If this script runs within the HTML file, then you can select elements from the document. Use

$("<select your input>").val() 

if it is a text box or .html() if it is an element.
To request the return info of your service, do not use jquery $. it was not built to ajax.

  1. make sure it is always up and running when you use the platform
  2. Instead of hosting it on localhost, try running it on whatever platform this js is on.
  3. make the localhost only display the info you are sending back
  4. send an ajax call to your localhost website, use
$.ajax({
url:"https://localhost:5001/mywebservice",
success:function(d){
//d is your data
}
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文