【问题标题】:jquery select alerts the wrong drop-down menu option value for on-change js behaviorjquery select 提示错误的下拉菜单选项值,用于更改 js 行为
【发布时间】:2017-06-10 01:55:30
【问题描述】:

我有一个保存动态菜单值的 Js 数组。菜单的选项值随着事件的 ID 而变化。

有一个包含表单的 jQuery 弹出窗口。加载窗口后,会显示菜单选项,但 jquery 不会提醒正确选择的值...它始终显示选择选项中的第一项

转到以下链接并单击事件 id=130131CHcbd 和 150724CH5f1 的“加入事件”按钮,您会看到没有选择什么事件日期,它会提醒相同的日期...

http://www.weg2g.com/application/abovezest/personal_networking.php

我有 JS 的基本知识,所以任何建议或帮助来解决这个问题...

<script> 
var jArray= {"150724CH5f1":"<option value=\"2017-01-27\" id=\"1\"> Fri Jan 27th 2017<\/option>  <option value=\"2017-02-03\" id=\"2\"> Fri Feb 3rd 2017<\/option>  <option value=\"2017-02-10\" id=\"3\"> Fri Feb 10th 2017<\/option>  <option value=\"2017-02-17\" id=\"4\"> Fri Feb 17th 2017<\/option>  <option value=\"2017-02-24\" id=\"5\"> Fri Feb 24th 2017<\/option>","130131CHcbd":"<option value=\"2017-01-28\" id=\"1\"> Sat Jan 28th 2017<\/option>  <option value=\"2017-02-04\" id=\"2\"> Sat Feb 4th 2017<\/option>  <option value=\"2017-02-11\" id=\"3\"> Sat Feb 11th 2017<\/option>  <option value=\"2017-02-18\" id=\"4\"> Sat Feb 18th 2017<\/option>  <option value=\"2017-02-25\" id=\"5\"> Sat Feb 25th 2017<\/option>"}; //get all 
function bind_event_dates(v,type)
{	
	//alert(jArray[v]);
	var w=jArray[v];
	//var y="'"+v+"'";
	
	 if (type==3)
	{
	var str1='<select name="edate" id="edate_bn_d" onchange="check_e_date(3);" required>';
	} //
	
	
	//append the other parts
	var str2=str1.concat(w);
	var str3='</select>';
	var kk=str2.concat(str3);
	
	//alert(rrr);
	$("#load_dates3").html(kk);
	
}//end of binding event dates
//--------------------------------------
  function check_e_date(a)
{
		 if (a==3)
		
		{
		var wanted=$("#edate_bn_d").val();
          //always select the first option of select menue
		alert(wanted);
		$("#bn_roomatte").val(wanted)
		}
		


}//end of check event date function
//-----------------------------------------------------------------------------	
//
</script> 

【问题讨论】:

标签: javascript jquery html drop-down-menu jquery-selectors


【解决方案1】:

我有编辑代码 javascript。它有工作。

var jArray= {"150724CH5f1":"<option value=\"2017-01-27\" id=\"1\"> Fri Jan 27th 2017<\/option>  <option value=\"2017-02-03\" id=\"2\"> Fri Feb 3rd 2017<\/option>  <option value=\"2017-02-10\" id=\"3\"> Fri Feb 10th 2017<\/option>  <option value=\"2017-02-17\" id=\"4\"> Fri Feb 17th 2017<\/option>  <option value=\"2017-02-24\" id=\"5\"> Fri Feb 24th 2017<\/option>","130131CHcbd":"<option value=\"2017-01-28\" id=\"1\"> Sat Jan 28th 2017<\/option>  <option value=\"2017-02-04\" id=\"2\"> Sat Feb 4th 2017<\/option>  <option value=\"2017-02-11\" id=\"3\"> Sat Feb 11th 2017<\/option>  <option value=\"2017-02-18\" id=\"4\"> Sat Feb 18th 2017<\/option>  <option value=\"2017-02-25\" id=\"5\"> Sat Feb 25th 2017<\/option>"}; //get all 
function bind_event_dates(v,type)
{	
	//alert(jArray[v]);
	var w=jArray[v];
	//var y="'"+v+"'";
	
	 if (type==3)
	{
	var str1='<select name="edate" id="edate_bn_d" onchange="check_e_date(3);" required>';
	} //
	
	
	//append the other parts
	var str2=str1.concat(w);
	var str3='</select>';
	var kk=str2.concat(str3);
	
	//alert(rrr);
	$("#load_dates3").html(kk);
	
}//end of binding event dates
//--------------------------------------
  function check_e_date(a)
{
	    if (a==3)		
		{
		var wanted=$("#edate_bn_d").val();
          //always select the first option of select menue
		alert(wanted);
		$("#bn_roomatte").val(wanted)
		}
		


}//end of check event date function
//-----------------------------------------------------------------------------	
//
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">      
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>Domoos mobile</title>
    <meta http-equiv="refresh" content="600">
    <meta http-equiv="pragma" content="no-cache">	
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

</head>
<body onload="bind_event_dates('150724CH5f1',3);">
	<div id="load_dates3">
	</div>
</body>

</html>

【讨论】:

  • 您添加了页面加载行为。但是,当使用单击按钮时,jQuery 窗口会打开,然后会填充菜单选项。选项被完美填充;但是,它确实选择了正确的值。您是否看到指向警报提供错误值的站点的链接..
  • 请注意,它在没有 jArray 且表单选项由 php 填充的事件详细信息页面上完美运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 2017-08-01
  • 1970-01-01
  • 2011-02-16
相关资源
最近更新 更多