2012-04-18 44 views
0

是否可以只从表单提交一些属性?只从表格中提取几个属性

<html><body> 
<FORM name="foo" id="foo" onSubmit="dosubmit();return false;"> 
    <INPUT type="text" name="s" id="s"><BR/> 
    <INPUT type="text" name="s2" id="s2"><BR/> 

    <select name="si" id="si"> 
    <option>SomeOption_1</option> 
    <option>SomeOption_2</option> 
    <option>SomeOption_3</option> 
    </select><BR/> 
<input type="submit" value="submit" id="postDataSubmit"> 
</FORM> 

<script> 
function dosubmit() 
{ 
    //Extract value of S2 and the chosen option (si) from the form here, and nothing else. Show them in an alert. 
} 
</script> 

</body></html> 
+0

您需要在最后切换html和body的结束标记位置。 – 2012-04-18 15:38:43

+0

@LarryBattle哈哈很好的接收。我在提出问题的同时添加了这些内容,并没有真正关注。固定。 – natli 2012-04-18 15:45:29

回答

1
function dosubmit() 
{ 
    alert(document.getElementById('s2').value); 
    alert(document.getElementById('si').options[document.getElementById('si').selectedIndex].value); 
} 

顺便说一下,如果这是你想要做的一切,那么你并不需要一个形式。使用纯JavaScript(使用jQuery是恕我直言,不是这个简单的任务必须的),你可以 简单的写

<button onclick="dosubmit()">Show values</button> 
+0

Lol @ downvote。 [没有足够的jQuery](http://www.doxdesk.com/img/updates/20091116-so-large.gif),我猜? – Travesty3 2012-04-18 14:42:47

+0

这是一个很好的答案,我从来没有要求过jQuery。谢谢! – natli 2012-04-18 14:48:03

+0

@natli:不能同意更多。你看我的评论中的链接?我觉得它很搞笑。 – Travesty3 2012-04-18 14:49:52

3

alert(document.forms.foo.s2.value); 
alert(document.forms.foo.si.value); 

因为你已经使用的名称,你可以用任何常规的按钮做到这一点对于每个输入,您可以通过document.forms访问表单上的所有元素;