2012-11-07 26 views
0
<form id="check" action="." method="POST"> 
    <input id="act" type="hidden" name="action"/> 
     <select id="blk"> 
      <option value="blk">Bulk Actions</option> 
      <option value="tes">Active</option> 
      <option>Inactive</option> 
      <option>Delete</option> 
     </select> 
    <input type="submit" value="Submit" onclick="document.getElementById('act').value = 'document.getElementById('blk').selectedIndex.value'"/> 
</form> 

選擇選項我要得到通過選擇標籤和值的行動將改變輸入名稱爲行動多動作的形式通過用JavaScript

+1

將會有什麼行動選擇選項之後,因爲應該有,當然你的目標文件 – polin

回答

1

你混合兩個概念的價值的價值。

  • select元素有一個value屬性,反映了當前選擇
document.getElementById('blk').value 
  • select元素與值反映的位置的選項列表和selectedIndex財產目前的選擇。使用這樣的:
var sel = document.getElementById('blk'); 
if (sel.selectedIndex >= 0) // could be -1 as well 
    return sel.options[sel.selectedIndex].value 

然而,實際上似乎想要什麼

<form id="check" action="." method="POST"> 
    <select name="action"> 
     <option value="blk">Bulk Actions</option> 
     <option value="tes">Active</option> 
     <option>Inactive</option> 
     <option>Delete</option> 
    </select> 
    <input type="submit" value="Submit" /> 
</form> 

您忘記您select控制name,所以沒有價值將被張貼。有了它,你不需要一個隱藏的輸入,也不需要按鈕上的點擊監聽器(它本應該也是一個onsubmit偵聽器)。

+0

是的,我只是想要得到改變這種「<輸入ID =」行爲」類型=‘隱藏’ name =「action」/>「value by select tag,只需提交按鈕組合 – abadi

+0

是的,然後使用'onclick =」document.getElementById('act')。value = document.getElementById('blk')。value「 - 沒有'selectedIndex',沒有撇號。此外,您可能會使用一個'',您的提交按鈕將提交(並重新加載)該頁面的點擊。 – Bergi

+0

仍然不適合我,請您請諒解,舉個例子 – abadi

0
try this 
<form id="check" action="." method="POST"> 
    <input id="act" type="hidden" name="action"/> 
     <select id="blk" onchange="javascript:CallMe(this.value)"> 
      <option value="blk">Bulk Actions</option> 
      <option value="tes">Active</option> 
      <option>Inactive</option> 
      <option>Delete</option> 
     </select> 
<input type="submit" value="Submit" onclick="document.getElementById('act').value = 'document.getElementById('blk').selectedIndex.value'"/> 
</form> 

<script> 
function CallMe(String str) 
{ 
alert(str+" : is the value selected , call the according action now"); 
} 
</script>