英文:
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:
<input type="submit" name="uwp_register_submit" class="form-control btn btn-primary btn-block text-uppercase uwp_register_submit">
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('input');
button.addEventListener('click', updateButton);
function updateButton() {
if (button.value === '') {
button.value = 'Register';
}
}
I realise this would only take effect on click, so when the button is pressed. So tried something like:
const button = document.querySelector('input');
if (button.value === '') {
button.value = 'Register';
}
Again, no success.
And I also saw something about using jquery? So tried the following without success.
$('.btn').val('Register');
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('input');
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("DOMContentLoaded", function(event) {
document.querySelector("[name='uwp_register_submit']").value = "Register";
});
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论