2013-04-09 58 views
1

在我的網站有,我給這樣的爲什麼我的腳本不能獲得價值,我的目的

<?php 
include 'config/koneksi.php'; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Lay Out Penempatan Produk UHT Area Gudang Rak A-F</title> 
<link rel="icon" href="http://localhost/wifi/images/RSUP.png"> 
<link rel="stylesheet" type="text/css" href="css/popup-window.css" media="screen"> 

<script type="text/javascript" src="js/popup-window.js"></script> 
<script type="text/javascript" src="js/jquery-1.8.3.js"></script> 



<script> 
    function setvalue(values) { 
    document.getElementById('posisi').value = values; 
} 
</script> 

<script type="text/javascript"> 
var htmlobjek; 
$(document).ready(function(){ 
    //apabila terjadi event onchange terhadap object <select id=pid> 
    $("#pid").change(function(){ 
    var pid = $("#pid").val(); 
    $.ajax({ 
     url: "ambilnamaproduk.php", 
     data: "pid="+pid, 
     cache: false, 
     success: function(data){ 
     $("#pname").val(data); 
     } 
     }); 
    }); 
    }); 
</script> 

<script> 
var i = $("#myTD").val(); 
    alert(i); 
</script> 

<script type="text/javascript"> 
var htmlobjek; 
$(document).ready(function() { 
var i = $("#myTD").val(); 
     $.ajax({ 
      url: "cek.php", 
      type:'POST' 
      data: "i="+i, 
      cache: false, 
      success: function (data) { 
       $("#cek").val(data); 
      } 

     });    
}); 
var k = $("#cek").val(); 
if(k = "0"){ 
$(document).ready(function() { 
       $(".data").css("background-color", "#00CC00");//green 
       }); 
       } 
    else{ 
$(document).ready(function() { 
       $(".data").css("background-color", "#FF0000");//red 
       }); 
}     
</script> 

</head> 
<body> 
<div> 
<input id="cek" name="cek" type="text" /> 

<table width="1023" border="1"> 
    <tr> 
    <th colspan="2">A1</th> 
    <th colspan="2">A2</th> 
    <th colspan="2">A3</th> 
    <th colspan="2">A4</th> 
    <th colspan="2">A5</th> 
    <th colspan="2">A6</th> 
    <th colspan="2">A7</th> 
    <th colspan="2">A8</th> 
    <th colspan="2">A9</th> 
    </tr> 
    <tr> 
    <td class="data"> <div align="center"> <input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" id="myTD" name="myTD" value="A1.4" /></div></td> 
    <td class="data"><div align="center"><input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" value="A1.8" /></div></td> 
    <td><div align="center"><input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" value="A2.4" /></div></td> 
    <td><div align="center"><input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" value="A2.8" /></div></td> 
    <td><div align="center"><input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" value="A3.4" /></div></td> 
    <td><div align="center"><input type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this, width : '270px' });setvalue(this.value);" value="A3.8" /></div></td> 

    <td><div align="center">A4.4</div></td> 
    <td><div align="center">A4.8</div></td> 
    <td><div align="center">A5.4</div></td> 
    <td><div align="center">A5.8</div></td> 
    <td><div align="center">A6.4</div></td> 
    <td><div align="center">A6.8</div></td> 
    <td><div align="center">A7.4</div></td> 
    <td><div align="center">A7.8</div></td> 
    <td><div align="center">A8.4</div></td> 
    <td><div align="center">A8.8</div></td> 
    <td><div align="center">A9.4</div></td> 
    <td><div align="center">A9.8</div></td> 
    </tr> 
    <tr> 
    <td><div align="center">A1.3</div></td> 
    <td><div align="center">A1.7</div></td> 
    <td><div align="center">A2.3</div></td> 
    <td><div align="center">A2.7</div></td> 
    <td><div align="center">A3.3</div></td> 
    <td><div align="center">A3.7</div></td> 
    <td><div align="center">A4.3</div></td> 
    <td><div align="center">A4.7</div></td> 
    <td><div align="center">A5.3</div></td> 
    <td><div align="center">A5.7</div></td> 
    <td><div align="center">A6.3</div></td> 
    <td><div align="center">A6.7</div></td> 
    <td><div align="center">A7.3</div></td> 
    <td><div align="center">A7.7</div></td> 
    <td><div align="center">A8.3</div></td> 
    <td><div align="center">A8.7</div></td> 
    <td><div align="center">A9.3</div></td> 
    <td><div align="center">A9.7</div></td> 
    </tr> 
    <tr> 
    <td><div align="center">A1.2</div></td> 
    <td><div align="center">A1.6</div></td> 
    <td><div align="center">A2.2</div></td> 
    <td><div align="center">A2.6</div></td> 
    <td><div align="center">A3.2</div></td> 
    <td><div align="center">A3.6</div></td> 
    <td><div align="center">A4.2</div></td> 
    <td><div align="center">A4.6</div></td> 
    <td><div align="center">A5.2</div></td> 
    <td><div align="center">A5.6</div></td> 
    <td><div align="center">A6.2</div></td> 
    <td><div align="center">A6.6</div></td> 
    <td><div align="center">A7.2</div></td> 
    <td><div align="center">A7.6</div></td> 
    <td><div align="center">A8.2</div></td> 
    <td><div align="center">A8.6</div></td> 
    <td><div align="center">A9.2</div></td> 
    <td><div align="center">A9.6</div></td> 
    </tr> 
    <tr> 
    <td><div align="center">A1.1</div></td> 
    <td><div align="center">A1.5</div></td> 
    <td><div align="center">A2.1</div></td> 
    <td><div align="center">A2.5</div></td> 
    <td><div align="center">A3.1</div></td> 
    <td><div align="center">A3.5</div></td> 
    <td><div align="center">A4.1</div></td> 
    <td><div align="center">A4.5</div></td> 
    <td><div align="center">A5.1</div></td> 
    <td><div align="center">A5.5</div></td> 
    <td><div align="center">A6.1</div></td> 
    <td><div align="center">A6.5</div></td> 
    <td><div align="center">A7.1</div></td> 
    <td><div align="center">A7.5</div></td> 
    <td><div align="center">A8.1</div></td> 
    <td><div align="center">A8.5</div></td> 
    <td><div align="center">A9.1</div></td> 
    <td><div align="center">A9.5</div></td> 
    </tr> 
</table> 
</div> 

<div class="popup_window_css" id="sample"/> 
<table class="popup_window_css"/> 
<tr class="popup_window_css"/> 
<td class="popup_window_css"/> 
<div class="popup_window_css_head"><img src="images/close.gif" alt="" width="9" height="9" />Aksi</div> 
<div class="popup_window_css_body"/><div style="border: 1px solid #808080; padding: 6px; background: #FFFFFF;"/> 
<form method="post" action="aksi.php"/> 
<table> 
    <tr> 
    <td> Werehouse Product </td> 
    <td><select name="wp"> 
     <option selected>- Jenis Werehouse -</option> 
     <?php 
     $q = mssql_query("select WHSName from tblSPDMstWarehouseProduct Order by WHSID"); 

     while ($r = mssql_fetch_array($q)){ 
     echo "<option value=$r[WHSName]>$r[WHSName]</option>"; 
     } 
     ?> 
</select> 
</td> 
    <tr> 
    <td> Posisi</td> 
    <td> <input type='text' name='posisi' id='posisi' readonly/></td> 
    </tr> 
    <tr> 
    <td> Product ID</td> 
    <td> <select name="pid" id="pid"> 
     <option selected = "selected" >- Produk ID -</option> 
      <?php 
      $q = mssql_query("select ProductID from tblMstProduct Order by ProductID"); 

      while ($r = mssql_fetch_array($q)){ 
      echo "<option value=$r[ProductID]>$r[ProductID]</option>"; 
     }?> 
     </select> 
    </td> 
    </tr> 
    <tr> 
    <td> Product Name</td> 
    <td ><input type="text" id="pname" name="pname" size="65" readonly/> 
    </td> 
    </tr> 
    <tr> 
    <td> Production Date</td> 
    <td > <input type='text' name="tgl" /></td> 
    </tr> 
    <tr> 
     <td colspan='2' align='center'> 
     <input type='submit' value= 'Save'> 
     <input type='button' value='view' onclick="javascript:ajax_post();"/> 
     </td> 
    </tr> 
</table> 
</from> 
<div class="popup_window_css_foot"> 
</div> 
</body> 
</html> 

,然後 使用jQuery選擇器來獲取價值和使用AJAX的靜態值表的內容執行該值這樣

var htmlobjek; 
    $(document).ready(function() { 
     var i = $("td").find("td.data").val(); 
      $.ajax({ 
       url: "cek.php", 
       data: "i", 
       cache: false, 
       success: function (data) { 
        $("#cek").val(data); 
       } 
      });  
    }); 

在當然cek.php這樣

<?php 
$posisi = $_POST[i]; 

$val = mssql_num_rows(mssql_query("SELECT*FROM tblTrnProduct WHERE Posisi = '$posisi'")); 

echo"$val"; 
?> 

根據以前的答案,是的,它的工作。但在單桌。我檢查了我的html結構(沒關係)。在我修改成複雜表後,同樣的情況再次發生。

+0

@PeeHaa埽你可以做到這一點,但不建議。相反,他應該使用'data-value =「」'..但是這隻在HTML5 – Brainfeeder 2013-04-09 07:35:12

+0

有效,我沒有看到OP設置了值@brain你是對的。 – PeeHaa 2013-04-09 07:37:26

+0

爲什麼你沒有得到像這樣的值.. $(「。data」)。attr(「value」).. – Dinesh 2013-04-09 07:39:14

回答

1

<td class="data">你忘了</tr>後。

$(document).ready(function(){ 

    function setvalue(values) { 
     $('#posisi').val(values); 
    } 

    var htmlobjek; 
    //apabila terjadi event onchange terhadap object <select id=pid> 
    $("#pid").change(function(){ 
     var pid = $("#pid").val(); 
     $.ajax({ 
      url: "ambilnamaproduk.php", 
      data: "pid="+pid, 
      cache: false, 
      success: function(data){ 
       $("#pname").val(data); 
      } 
     }); 
    }); 

    var i = $("tr").find("td.data").attr('value'); 
    $.ajax({ 
     url: "cek.php", 
     type:'POST' 
     data: {"data": i}, 
     cache: false, 
     success: 
     function (data) { 
      $("#cek").val(data); 
     } 
    }); 

    var k = $("#cek").val(); 
    if(k = "0"){ 
     $(".data").css("background-color", "#00CC00");//green 
    } 
    else { 
     $(".data").css("background-color", "#FF0000");//red 
    } 

}); 

在cek.php中,您的'我'應該是可用的$_POST['data']

供參考: 如果您有多個行的值或多個表的行中有一個值,您應該寫代碼不同。請參閱this jsFiddle

但是,如果有很多值,會有很多ajax調用。 更好的解決方案是將值添加到隱藏的輸入。請參閱this jsFiddle

希望它能幫助你。

+0

這個答案只適用於單一表格。 – Andriansyah 2013-04-10 03:28:39

+0

做單行嗎?或單桌?如果您需要頁面上的每個td值,您可能應該爲每個循環放置ajax。 – Brainfeeder 2013-04-10 06:39:29

+0

我的意思是在我的代碼中的單個表格和單個行中 – Andriansyah 2013-04-11 02:59:17

0

首先嚐試解決此問題:

var i = $("td").find("td.data").val(); 
... 
data: "i", 

var i = $("td").find(".data").attr('value'); 
... 
data: "i="+i, 
+0

不工作... – Andriansyah 2013-04-09 08:17:09

+0

@AndriansyahAndri如果你嘗試'變種I = $( 「TR」)找到( 「td.data」)ATTR( '值');' – Brainfeeder 2013-04-09 08:18:43

+0

@Brainfeeder沒有什麼變化.. 。:( – Andriansyah 2013-04-09 08:33:49

0

只是試試這個=

$.ajax({ 
       url: "cek.php", 
       data: { "data": i}, 
       cache: false, 
       success: function (data) { 
        $("#cek").val(data); 
       } 
      }); 

,並在PHP 你可以做象下面這樣:

$data = $_POST['data']; //you will get your value 
+0

不工作.. :( – Andriansyah 2013-04-09 07:52:46

+0

@AndriansyahAndri:首先它應該工作,如果不只是試圖把它像「數據:{‘數據’ :我},」還更新了我的帖子 – 2013-04-09 07:57:52

+0

還是不行.. :( – Andriansyah 2013-04-09 08:13:20

1

試試這個,希望它有用

var j = $("td").find("td.data").val(); //make sure this will returning value 
$.ajax({ 
     url: "cek.php", 
     type:"POST", 
     data: { i: j }, //Or try { i: $("td").find("td.data").val()}, 
     cache: false, 
     success: function (data) { 
      $("#cek").val(data); 
     } 
});  

在PHP

$posisi = $_POST['i']; 

例如形式jQuery website

$.post("test.php", { name: "John", time: "2pm" }) 
    .done(function(data) { 
     alert("Data Loaded: " + data); 
    }); 
+0

我曾經嘗試都,但它不工作:( – Andriansyah 2013-04-09 08:15:17

0

有你的JavaScript函數 1)您的變量兩個主要問題

data: "i", 

應該

data: i, 

2)其次,你沒有提到的AJAX方法的類型羯羊它是GET或POST,在AJAX默認類型會得到,如果你不提它。因此,在你的情況下,你傳遞的類型是GET,並嘗試訪問變量作爲POST。所以這行添加到您的AJAX調用

type:'POST', 
+0

不起作用:( – Andriansyah 2013-04-09 07:53:05

相關問題