簡體   English   中英

使用JavaScript獲取HTML表單輸入

[英]Using javascript to take HTML form inputs

我想使用javacript來簡單地查看特定的HTML表單(即單選按鈕),找到用戶的選擇,然后以各種方式進行響應。

javascript有沒有辦法讀取用戶輸入或表單的選定響應?

是的,有很多方法可以做到這一點。

您可以在更改時檢查用戶輸入。 下面是其中之一。

 <input type="text" onchange="changed(this);"/>

此輸入將以self為參數調用changed()函數。 因此,您可以控制其價值並做一些事情。

function changed(input){
  alert(input.value)  // check for input value
  if(input.value.length > 54){ //do if value is longer than 54 characters
     //Do something
  }
}

您可以在互聯網上找到很多方法。 您可以嘗試使用jQuery,這對您來說會更容易。

$("#myInput").change(function(){   //select the input box which id is 'myInput' then run function when its value changed each time
    alert("Wow, my value changed to:"+$(this).val())   //Get value with $(this).val() and alert!
});

您也可以使用$("input").change(function(){


根據您的要求更改我的答案。

<form name="myForm">
   <!--Lots of divs here with input. You must hide these divs without touching form! -->
</form>

在最后一頁上,您需要做的就是

function listUserValues(){
   var els=document.myForm.elements;
   var l=els.length;
   for (var i=0; i<l; i++)
   {
      alert('Field Name: '+els[i].name+'\nField Type: '+els[i].type+'\nField Value: '+els[i].value);
      //Do whatever you want with each value here.
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM