为没有值的HTML标签创建一个不同的值的输入按钮。

huangapple go评论75阅读模式
英文:

Give an input button with no value HTML tag a different value

问题

我正在使用WordPress中的UserWP插件,在我的注册页面上有一个表单和一个按钮,上面写着"Submit Query",但这不是最好的文本。我更想用"Register"。

在设置中没有更改按钮文本的选项,因此尝试利用另一种方法。

按钮的HTML字符串如下:

<input type="submit" name="uwp_register_submit" class="form-control btn btn-primary btn-block text-uppercase uwp_register_submit">

所以没有"value"用于文本。

我的目标是"注入"值"Register"到HTML字符串中。我认为可以使用JavaScript片段来实现这一点,但我不是很擅长JS,所以我很难实现它。

搜索了很多,找到了一些JS代码,可以查找标识符中的文本字符串并替换它,但是我似乎无法使其工作。

const button = document.querySelector('input');

button.addEventListener('click', updateButton);

function updateButton() {
  if (button.value === '') {
    button.value = 'Register';
  }
}

我意识到这只有在点击按钮时才会生效,所以尝试了类似的方法:

const button = document.querySelector('input');

if (button.value === '') {
    button.value = 'Register';
  }

再次没有成功。

我还看到了关于使用jQuery的内容?所以尝试了以下方法,但没有成功。

$('.btn').val('Register');

我只想针对按钮的类,告诉它将值设置为"Register" - 对于某人来说,这可能是一个简单的解决方案,但对我来说不是...

英文:

I'm using UserWP plugin in WordPress and on my registration page I have the form and the button which says "Submit Query" - not the best text. I'd much rather have "Register".

There's no option in the settings to change the button text so trying to utilise another method.

The HTML string for the button is:

&lt;input type=&quot;submit&quot; name=&quot;uwp_register_submit&quot; class=&quot;form-control btn btn-primary btn-block text-uppercase uwp_register_submit&quot;&gt;

So there is no "value" for the text.

My aim is to "inject" the value="Register" into the HTML string. I believe this can be done with a JavaScript snippet, but not being the best at JS, I'm struggling to achieve it.

Lots of googling found some JS code that finds a text string in the identifier and replaces it, however I cannot seem to get this working.

const button = document.querySelector(&#39;input&#39;);

button.addEventListener(&#39;click&#39;, updateButton);

function updateButton() {
  if (button.value === &#39;&#39;) {
    button.value = &#39;Register&#39;;
  }
}

I realise this would only take effect on click, so when the button is pressed. So tried something like:

const button = document.querySelector(&#39;input&#39;);

if (button.value === &#39;&#39;) {
    button.value = &#39;Register&#39;;
  }

Again, no success.

And I also saw something about using jquery? So tried the following without success.

$(&#39;.btn&#39;).val(&#39;Register&#39;);

I simply want to target the class of the button and tell it to give a value of "Register" - I bet it's an easy solution for someone, but not me...

答案1

得分: 0

以下是翻译好的部分:

"document.querySelector('input');" 给你返回第一个匹配的元素。请参阅MDN Document.querySelector

所以,如果有多个输入字段,它将获取第一个输入字段,这可能不是您的提交输入。

另一种方法是搜索输入字段的特定名称。

document.addEventListener("DOMContentLoaded", function(event) {
   document.querySelector("[name='uwp_register_submit']").value = "Register";
});

如果该字段存在多次,请使用querySelectorAll并与循环结合使用。


在您的特定情况下,也许更好的方法是更改英语的默认翻译。请参阅文档。看起来比实际复杂要简单一些。

英文:

In general you can achieve that by getting the element and setting the value as you try.

But the line document.querySelector(&#39;input&#39;); gives you back the first matching element back. See MDN Document.querySelector

So if there are more than one input fields, it will take the first input, which may not be your submit input.

An alternative would be to search for the specific name of the input field.

document.addEventListener(&quot;DOMContentLoaded&quot;, function(event) {
   document.querySelector(&quot;[name=&#39;uwp_register_submit&#39;]&quot;).value = &quot;Register&quot;;
});

If that field exists multiple times, use querySelectorAll and combine it with an loop.


In your specific case it would maybe the better way to change the default translation for english. Have a look at the documentation. It looks more complicated as it is.

huangapple
  • 本文由 发表于 2023年2月14日 20:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定