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>
4

2 回答 2

3

使用纯javascript(恕我直言,这个简单的任务不需要使用JQuery)你可以简单地写:

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

因为您为每个输入使用了名称,所以您可以通过 document.forms 访问表单上的所有元素;

于 2012-04-18T14:50:05.327 回答
1
function dosubmit()
{
    alert(document.getElementById('s2').value);
    alert(document.getElementById('si').options[document.getElementById('si').selectedIndex].value);
}

顺便说一句,如果这就是您想要做的全部,那么您不需要表格。您可以使用任何常规按钮执行此操作:

<button onclick="dosubmit()">Show values</button>
于 2012-04-18T14:34:49.777 回答