簡體   English   中英

獲取選擇選項的值不起作用

[英]Getting the value of select option not working

我有一個帶有2個選項的下拉列表(本地和導入)。 如果選擇LOCAL,則應該在IMPORT時獲得LOC值,而應該獲得IM。 但是,無論我選擇了哪兩個,我都會得到IM值。 請幫助,如何獲取相應選擇選項的值。謝謝。

<?php  $shipmenttype = array("0"=>"Please select", "LOC"=>"LOCAL", "IM"=>"IMPORT");?>
<td><b><select name='shiptype' id='shiptype' onchange=''>
    <?php foreach($shipmenttype as $keyname=>$ship_type):?>

        <?php 
            $selected = "";
                    $shiptype = $_POST['shiptype'];
                    if($keyname == $shiptype): $selected =" selected"; endif;       
        ?>                    
                <option value="<?php echo $keyname;?>" <?php echo $selected;?>><?php echo $ship_type;?></option>
            <?php endforeach;?>

</select></b></td></tr>


<?php

$site = dbGetConfig("sitecode");
$dbnextID = dbNextID($keyname);

if($site=="CKI") {
$site="CK".date("Y");
}
if($site=="PQI") {
$site="PQ".date("Y");
}

$refnumb = $keyname.$site."-".str_pad($dbnextID,7,0, STR_PAD_LEFT); 

?>

運行編號(000000):

function dbNextID($key) {
$sql1 = "insert into key_master (keyname) values (:keyname)";
$sql2= "update key_master set id = id + 1 where keyname = :keyname";
$sql3 = "select id from key_master where keyname = :keyname";

$conn = dbConnect(); 
$stmt1 = $conn->prepare($sql1);
$stmt2 = $conn->prepare($sql2);
$stmt3 = $conn->prepare($sql3);

$conn->beginTransaction();
$stmt1->execute(array(':keyname' => $key));
$stmt2->execute(array(':keyname' => $key));
$stmt3->execute(array(':keyname' => $key)); 
$value = $stmt3->fetchColumn(0);
$conn->commit();
$conn=null;

return $value;
}

HTML表格:

<!--- Local Shipment --->

<table id="t1" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship1' id='ship1' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><input type='text' name='ref_no1' id='ref_no1' value="<?php echo $refnumb ?>" readonly /></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier1'>
    <?php foreach($data1 as $row1): ?>
    <option value="<?php echo $row1['SupplierName'] ?>"><?php echo $row1['SupplierName'] ?></option>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description1' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no1' /></td></tr> 

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number1' /></td></tr>

<tr><th align='right'>Receive Qty: </th>
<td><input type='text' name='r_qty1' /></td></tr>

<tr><th align='right'>No. of Boxes/Packs: </th>
<td><input type='text' name='no_boxes1' /></td></tr>

<tr><th align='right'></th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='local_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='local_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>


<!--- Import Shipment --->

<table id="t2" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship2' id='ship2' value="" readonly /></b></td></tr>

<tr><th align='right'>Reference Number: </th>
<td><b><input type='text' name='ref_no2' id='ref_no2' value="<?php echo $refnumb ?>" readonly /></b></td></tr>

<tr><th align='right'>Supplier: </th>
<td><select name='supplier2'>
    <?php foreach($data2 as $row2): ?>
    <option value="<?php echo $row2['SupplierName'] ?>"><?php echo $row2['SupplierName'] ?>
    <?php endforeach; ?>
    </select></td></tr>

<tr><th align='right'>Description: </th>
<td><input type='text' name='description2' size='70' /></td></tr>

<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no2' /></td></tr>

<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number2' /></td></tr>

<tr><th align='right'>AWB(Airway Bill) No: </th>
<td><input type='text' name='awb_no2' /></td></tr>

    <tr><th align='right'>AWB No of Boxes/Packs: </th>
<td><input type='text' name='awb_no_boxes2' /></td></tr>

<tr><th align='right'> </th>
<td></td></tr>

<tr><th align='right'> </th>
<td align='left'><input type='submit' name='import_add' value='SAVE' style='font-size:2em;' /> &nbsp;&nbsp;&nbsp;&nbsp; <input type='submit' name='import_cancel' value='CANCEL' style='font-size:2em;' /></td></tr>
</table>

jQuery的:

<script>
$('#shiptype').change(function () {
$('#t1,#t2').hide();
    var selectedValue = $(this).val();
var strloc = "LOCAL";
var strimp = "IMPORT";

    if (selectedValue == "LOC") {
        $('#t1').show();
    $('#ship1').val(strloc);
    //$('#strkey').val(selectedValue);

    } else if (selectedValue == "IM") {
        $('#t2').show();
    $('#ship2').val(strimp);
    //$('#strkey').val(selectedValue);
    }           
 });

$('#t1,#t2').hide();

</script>

您可以在代碼中嘗試:

$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');

您的完整代碼(已修改):

<select name='shiptype' id='shiptype' onchange='' >
<?php foreach($shipmenttype as $keyname=>$ship_type):?>

<?php 
$shiptype = $_POST['shiptype'];
$selected = ($shiptype == $keyname ? 'selected=""' : '');
?>                    
<option value="<?php echo $keyname;?>" <?php echo $selected;?>>

<?php echo $ship_type;?>
</option>
<?php endforeach;?>

</select>

現有代碼的解決方案:

$selected = "";
if($keyname == $shiptype): $selected ='selected=""'; 
endif;       

我改變了什么?

只需替換:

$selected ="selected";

附:

$selected ='selected=""';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM