Create event with submit button
I need some help on coding my project. Can you please help me to finish my project. I need to create .click
event to anchor, the code will be as follows:
$("form").submit(function() {
if ($("input:first").val() == "something") {
// anchor tag should be clicked and input should find submitted target.
}
}
Shortly I want to create search 开发者_如何学Pythonform which creates click event and finds entered target.
Thank you for your assistance in advance!
If I understand your question correctly, you’re trying to jump to an anchor in the page based on what’s entered in the form.
$('form').submit(function(e) {
e.preventDefault();
if ($('input:first', this).val() == 'something') {
// anchor tag should be clicked and input should find submitted target.
window.location.hash = 'example-hash';
}
)};
If there are multiple options, you could use switch
:
$('form').submit(function(e) {
var val = $('input:first', this).val(),
hash;
e.preventDefault();
switch (val) {
case 'foo':
hash = 'some-hash';
break;
case 'bar':
hash = 'some-other-hash';
break;
default:
hash = 'default-hash';
break;
}
window.location.hash = hash;
)};
Assuming you want to override the default submit-behavior and click an anchor instead:
$("form").submit(function(e) {
if ($("input:first").val() == "something") {
$("#anchorelementId").trigger('click');
e.preventDefault(); // Prevents the submit-behavior from your form.
}
}
精彩评论