Home >Backend Development >PHP Tutorial >php模板中的select控件的onchang事件代码不起作用

php模板中的select控件的onchang事件代码不起作用

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:04:561019browse

php模板中的select控件的onchang事件代码不起作用,如下:

<script language="JavaScript">  function s_changed()  //cjq add  {		 alert("s_changed");                 var index=document.forms['theForm'].s[].selectedIndex;		 if ((document.forms['theForm'].s[].options[index].value=='2')                 {                    alert("ok");                    ...                 }  }</script><form name="theForm" action="o.php?act=s" method="post"><table>  <tr>    <td scope="col">1</td>    <td scope="col">2</td>  </tr>  {foreach from=$g1 item=g name="g"}	<tr>	<td>1</td>	<td><select name="s[]" onchange="JavaScript:s_changed()">      <option value=" ">请选择</option>	  {foreach from=$s1_list item=s}	       <option value="{$s.name}" {if $s.name eq  $s1}selected{/if}>{$s.name}</option>	  {/foreach}	  {/if}    </select>	    </td>    </tr>{/foreach}</table></form>

请问这是为什么?


回复讨论(解决方案)

<html><head><script type="text/javascript">function fun(x){  var y=document.getElementById(x).value;  alert(y);}</script></head><body>Enter your name: <input type="text" id="fname" onchange="fun(this.id)"><select id="select" name="s[]" onchange="fun(this.id)">  <option value=" ">请选择</option>  <option value="1">1</option>  <option value="1">2</option>  <option value="1">3</option></select> </body></html>

document.forms['theForm'].s[]
要写作
document.forms['theForm'] ["s[]"]

	function s_changed(obj) {		if (obj.value == '2') {			alert('ok');		}	}


<select name="s[]" onchange="JavaScript:s_changed(this)">

谢谢斑竹!!!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn