简体   繁体   English

在提交表单之前获取表单的选择列表值

[英]Getting form’s select list values before form’s submission

I want to get the values from selected “select list option fields” in a form, before submit the form, and use them as variables in my web page. 在提交表单之前,我想从表单中选定的“选择列表选项字段”中获取值,然后将其用作我的网页中的变量。

For example: I have the form bellow: 例如:我的形式如下:

<form>
<select name="test">
<option value="111">Something</option>
<option value="222">Something</option>
<option value="333">Something</option>
</select>
</form>

I want to know if my web page user has selected the 111 or the 222 or the 333 value and after that to do something like that: 我想知道我的网页用户是否选择了111或222或333值,然后再执行以下操作:

<%
If user selected the value "111" then 
Do something...
Else
Do something else...
End If
%>

Is there any possible way to do something like that before form's submission? 有没有可能在提交表单之前做类似的事情?

After a long search I did, I learned that what I need can be done with JavaScript, but unfortunately I am completely ignorant with JavaScript. 经过长时间的搜索,我了解到可以使用JavaScript来完成所需的工作,但不幸的是,我对JavaScript完全一无所知。 Could anyone guides me? 有人可以引导我吗?

You can't use form values as ASP variables without submitting them to the server because ASP runs on the server to do all it's stuff. 如果不将表单值提交给服务器,则不能将其用作ASP变量,因为ASP在服务器上运行以完成所有工作。 JavaScript will run in the browser so can perform functions such as calculations without submitting the form to the server. JavaScript将在浏览器中运行,因此可以在不将表单提交给服务器的情况下执行诸如计算之类的功能。

If you simply want to display the selected form options on the page then us can use JavaScript (or jQuery if you have that running on the page). 如果您只是想在页面上显示选定的表单选项,那么我们可以使用JavaScript(如果页面上运行的是jQuery,则可以使用jQuery)。 eg 例如

<script>
 function showValue(s) {
  document.getElementById('myid').innerHTML = s;
 }
</script>
 <form>
<select name="test" onChange="showValue(this.value);">
<option value="111">Something 111</option>
<option value="222">Something 222</option>
<option value="333">Something 333</option>
</select>
</form>
<div id="myid"></div>

If you want to process the selection in some way before submitting the form to either set another form field, or display some calculation you need to either write the calculation function in JavaScript or use AJAX (as suggested by ThatGuyInIT) to get another ASP page to do the processing for you and bring the results back to your page. 如果要在提交表单以设置另一个表单字段或显示某些计算之前以某种方式处理选择,则需要使用JavaScript编写计算功能或使用AJAX(如ThatGuyInIT的建议)来获取另一个ASP页面为您进行处理,然后将结果返回到您的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM