如何在选项字段中显示编辑表单和选定的值

时间:2016-10-08 08:02:57

标签: javascript php jquery html5

在这里我想做,有一个单击编辑按钮意味着,我想在同一页面和编辑表单中显示编辑表单我想要显示已编写的值,我要在该表单字段中显示的值,在这里我为卡车名称做了因为输入=文本这个,但是下一个字段我无法显示他已选择的状态,怎么做呢

<?php
$sql = "SELECT * FROM trucks WHERE status !='1' ORDER BY reg_date DESC";
$result = mysql_query($sql);
$cat=array();
while($row = mysql_fetch_array($result)){
	echo '<tr>
			<td>'.$row["truck_name"].'</td>
			<td>'.Getstate($row["state_id"]).'</td>
			<td>'.Getcity($row["city_id"]).'</td>
			
			<td class="col-medium center">
			<div class="btn-group btn-group-xs ">
			<a class="btn btn-inverse" style="cursor:pointer;" onclick="show_edit_menu(\''.$row["id"].'\',\''.$row["truck_name"].'\',\''.$row["state_id"].'\',\''.$row["city_id"].'\')"><i class="fa fa-edit icon-only"></i>Edit</a>
			<a class="btn btn-danger" onclick="truck_deletemenu('.$row["id"].')"><i class="fa fa-times icon-only"></i>Delete</a>
			</div>
			</td>
		</tr>';
}

?><?php
$sql = "SELECT * FROM trucks WHERE status !='1' ORDER BY reg_date DESC";
$result = mysql_query($sql);
$cat=array();
while($row = mysql_fetch_array($result)){
	echo '<tr>
			<td>'.$row["truck_name"].'</td>
			<td>'.Getstate($row["state_id"]).'</td>
			<td>'.Getcity($row["city_id"]).'</td>
			
			<td class="col-medium center">
			<div class="btn-group btn-group-xs ">
			<a class="btn btn-inverse" style="cursor:pointer;" onclick="show_edit_menu(\''.$row["id"].'\',\''.$row["truck_name"].'\',\''.$row["state_id"].'\',\''.$row["city_id"].'\')"><i class="fa fa-edit icon-only"></i>Edit</a>
			<a class="btn btn-danger" onclick="truck_deletemenu('.$row["id"].')"><i class="fa fa-times icon-only"></i>Delete</a>
			</div>
			</td>
		</tr>';
}

?>
  
  
  <script>
	function show_edit_menu(id,t_name,state_id,city_id){
	$("#show_edit_menu").show();
	
	$("#tid").val(id);//truck auto id
	$("#tname").val(t_name);//Truck Name
	$("#state").val(state_id);//state id
	
	console.log(t_name);//i got truck name value here
	console.log(state_id);//i got state name value here
	
	
	}
</script>	
<!--edit part here-->
<div class="portlet" id="show_edit_menu" style="display:none;">
<div class="portlet-heading dark">
<div class="portlet-title">
	<h4>Edit Menu</h4>
</div>
<div class="portlet-widgets">
	<a data-toggle="collapse" data-parent="#accordion" href="#f-3"><i class="fa fa-chevron-down"></i></a>
</div>
<div class="clearfix"></div>
</div>
<div id="f-3" class="panel-collapse collapse in">
<div class="portlet-body">
	<form class="form-horizontal" role="form" id="edit_form" method="post" enctype="multipart/form-data">
	
		<div class="form-group">
			<label class="col-sm-2 control-label">Truck Name<span class="require">*</span></label>
			<div class="col-sm-8">
				<input type="text" class="form-control" value="" name="tname" id="tname">
				<input type="hidden" class="form-control" value="" name="tid" id="tid">
			</div>
		</div>
		
		<div class="form-group">
					<label class="col-sm-2 control-label">State<span class="require">*</span></label>
					<div class="col-sm-8">
						 <select class="form-control intro-form-fixer state" required="" id="state" name="state" data-msg-required="Please enter your State" value="" aria-required="true">
						 <option value="1"> I want to show which state i selected</option>
					</select>
					<!--<input type="hidden" id="state" name="state" value="" />-->
					</div>
				</div>
		
		
		<div class="form-actions">
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="button" class="btn btn-primary" onclick="edit_menu()">Submit</button>
				</div>
			</div>
		</div>
	</form>
</div>
</div>
</div>
			

1 个答案:

答案 0 :(得分:0)

试试这个:

$("#state option[value='"+state_id+"']").attr("selected", true); 

而不是

$("#state").val(state_id);