英文:
Submitting form with hyperlink
问题
我有一个HTML5提交按钮,我想将其转换为用于提交的超链接。有简单的方法吗?
<button type="submit" name="signin" id="signin">发送</button>
我需要提交的新自定义提交按钮
<a href="#">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
发送测试
</div>
</a>
英文:
I have a HTML5 submit button that I want converted to a hyperlink for submitting. Is there a simple method for this?
<button type="submit" name="signin" id="signin"> Send </button>
New custom submit button that I need to submit from
<a href="#">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
Send-TEST
</div>
</a>
答案1
得分: 1
你可以在<a>标签上使用onclick属性,在单击超链接时执行JavaScript代码。JavaScript代码document.getElementById('signin').click(); 会触发原始提交按钮上的单击事件,从而提交表单。
如果您的提交按钮具有不同的ID,请确保将'signin'替换为相应的ID。
以下是一个示例:
<a href="#" onclick="document.getElementById('signin').click(); return false;">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
Send-TEST
</div>
</a>
英文:
you can use the onclick attribute on the <a> tag to execute JavaScript code when the hyperlink is clicked. The JavaScript code document.getElementById('signin').click(); triggers a click event on the original submit button, which will submit the form.
Make sure to replace 'signin' with the appropriate id if your submit button has a different ID.
Here is some example
<a href="#" onclick="document.getElementById('signin').click(); return false;">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
Send-TEST
</div>
</a>
答案2
得分: 1
To convert your HTML5 submit button to a hyperlink for submitting, you can use JavaScript to trigger the form submission when the hyperlink is clicked. I don't think anything is built in like Barmar mentioned in a comment.
Here's an example with JS:
HTML
<form id="myForm" action="your-action-url" method="POST">
<!-- Your form fields here -->
</form>
<a href="#" onclick="document.getElementById('myForm').submit(); return false;">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
Send-TEST
</div>
</a>
In this example, we have an HTML form with the ID myForm
, which contains your form fields. The action
attribute specifies the URL where the form data should be submitted.
The <a>
tag acts as a hyperlink, and the onclick
attribute triggers the JavaScript code when the hyperlink is clicked. The JavaScript code document.getElementById('myForm').submit()
accesses the form element and submits it.
By using this approach, clicking on the "Send-TEST" hyperlink will trigger the form submission, similar to clicking the original submit button. Make sure to replace 'your-action-url' in the form's action
attribute with the appropriate URL to handle the form submission on your server.
英文:
To convert your HTML5 submit button to a hyperlink for submitting, you can use JavaScript to trigger the form submission when the hyperlink is clicked. I don't think anything is built in like Barmar mention in a comment.
Here's an example with JS:
HTML
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<form id="myForm" action="your-action-url" method="POST">
<!-- Your form fields here -->
</form>
<a href="#" onclick="document.getElementById('myForm').submit(); return false;">
<div class="button-1 valign-text-middle roboto-medium-white-16px">
Send-TEST
</div>
</a>
<!-- end snippet -->
In this example, we have an HTML form with the ID myForm, which contains your form fields. The action attribute specifies the URL where the form data should be submitted.
The <a> tag acts as a hyperlink, and the onclick attribute triggers the JavaScript code when the hyperlink is clicked. The JavaScript code document.getElementById('myForm').submit() accesses the form element and submits it.
By using this approach, clicking on the "Send-TEST" hyperlink will trigger the form submission, similar to clicking the original submit button. Make sure to replace 'your-action-url' in the form's action attribute with the appropriate URL to handle the form submission on your server.
答案3
得分: 1
以下是您要翻译的内容:
在这里,我将锚元素与提交按钮放在标签元素内。通过点击锚元素,也触发了提交按钮。只要锚元素没有href属性,它就会作为纯文本呈现。因此,锚点需要样式化为锚点。
请注意,基本表单和使用锚点/标签提交它的功能不需要任何JavaScript。
<form name="form01">
<label>
<input type="submit">
<a>提交</a>
</label>
</form>
input[type='submit'] {
display: none;
}
input~a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
document.forms.form01.addEventListener('submit', e => {
e.preventDefault();
console.log('提交...');
});
英文:
Here I place the anchor element together with a submit button inside a label element. By clicking the anchor element the submit button is also triggered. This only works as long as the anchor element do not have a href attribute, and therefor it is rendered as a plain text. Consequentially the anchor needs styling as an anchor.
Notice that the basic form and the functionality of submitting it using the anchor/label do not require any JavaScript.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.forms.form01.addEventListener('submit', e => {
e.preventDefault();
console.log('submit...');
});
<!-- language: lang-css -->
input[type='submit'] {
display: none;
}
input~a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
<!-- language: lang-html -->
<form name="form01">
<label>
<input type="submit">
<a>Submit</a>
</label>
</form>
<!-- end snippet -->
答案4
得分: 0
只需使用CSS来将其样式设置成超链接样式,如果您希望提交按钮的行为看起来像超链接。
重置按钮的样式,然后将其样式设置为您希望的样子。
英文:
Just style it to make it look like a hyper link with CSS, if you wish for the behaviour of a submit button looking like a hyperlink.
Reset the styles for the button and style it the way you want it to look like.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论