2016-12-06 53 views
1

在这里我想要做的是,在更改状态我想获得值id和状态,在这里我只有id,我无法获得状态(1或2或3) ,我不知道该怎么做: -如何获取表格中的选定值

function get_pstatus(_this){ 
 
    var p_id=$(_this).closest('tr').find('#p_id').val(); 
 
    var p_status=$(_this).closest('tr').find('#p_status').text(); 
 
    console.log(p_status); \t \t 
 
}
<table class="table table-striped table-bordered table-hover dataTables-example" id=""> 
 
<thead> 
 
    <tr> 
 
     <th>No.</th> 
 
     <th>Project Name</th> 
 
     <th>Status</th> 
 
    </tr> 
 
</thead> 
 
<tbody> 
 
\t <?php 
 
\t include('dbconfig.php'); 
 
\t $project = mysql_query("SELECT id,project_id,project_name FROM add_projects WHERE status !='1'"); 
 
\t for ($i=1;$p=mysql_fetch_assoc($project);$i++){ 
 
\t ?> 
 
\t <tr class="odd gradeX"> 
 
\t \t <td id="s_no"><?php echo $i;?><input id="p_id" type="hidden" value="<?php echo $p['id']?>"></td> 
 
\t \t <td id="p_name"><?php echo $p['project_name']?></td> 
 
\t \t <td id="p_status"> 
 
\t \t \t <form style=" margin-bottom: 0px;" id="project_status"> 
 
\t \t \t <select class="form-control" id="status" name="status" onchange="get_pstatus(this);" style="width:150px;"> 
 
\t \t \t <option value="">-- Select Status --</option> 
 
\t \t \t \t <option value="1">one</option> 
 
\t \t \t \t <option value="2">two</option> 
 
\t \t \t \t <option value="3">three</option> 
 
\t \t \t </select> 
 
\t \t \t </form> 
 
\t \t </td> 
 
\t </tr> 
 
\t <?php } ?> 
 
</tbody> 
 
</table>

+0

发表您的jQuery以及 –

+0

在改变的状态到时候我想利用这个值像1或2或3,我也想采取ID值 –

回答

0

你需要获得所选择的选项的值和文本两种,所以做象下面这样: -

function get_pstatus(){ 
    var p_id = $('select#status').find('option:selected').val(); 
    var p_status = $('select#status').find('option:selected').text(); 
} 

或者

function get_pstatus(){ 
    var p_id = $('#status :selected').val(); 
    var p_status = $('#status :selected').text(); 
} 

而不是onchange="get_pstatus(this);onchange="get_pstatus();

例子: -

function get_pstatus(){ 
 
    var p_id = $('#status :selected').val(); 
 
    var p_status = $('#status :selected').text(); 
 
    console.log(p_id); 
 
    console.log(p_status); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select class="form-control" id="status" name="status" onchange="get_pstatus(this);" style="width:150px;"> 
 
    <option value="1">one</option> 
 
    <option value="2">two</option> 
 
    <option value="3">three</option> 
 
</select>

+0

p_status我geting像这个 - S选择状态 - –

+0

如何删除 - 选择状态 - - –

+0

这是一个选择框,因此当您选择不同的值时,会出现相应的文本。所以如果你不想要' - 选择状态--'。然后删除'' –