开发者

Selecting div's href's and submitting form?

I have this code in my html:

<div id="div1">
<a href="a"> Apple </a>
<a href="b"> Orange </a>
<a href="c"> Lemon </a>
<a href="d"> Banana </a>
</div>

<form action=" ">
<input type="text"开发者_如何学编程 name="q" id="input1" >
</form>

When ever I click anything in the div1 is set to the input thanks to @alex

Is it possible to submit the form too though?

My JavaScript looks like this:

$('#div1 > a').click(function(event) {
   event.preventDefault();
   $('#input1').val($(this).attr('href'));
});

I have a fiddle of how it looks like. http://jsfiddle.net/FCHpn/


This will submit the form which #input1 sits in.

$('#div1 > a').click(function(event) {
   event.preventDefault();
   $('#input1').val($(this).attr('href')).closest('form').submit();
});


To submit the form you just have to call .submit() on the form element. I would give the form an id and then call something like

$("#formid").submit();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜