2017-07-08 70 views
-3

我想使用td id查找最大值和最大值,但我必須採用文本框的值,我必須發送查找幫助我。我可以用javaScript文本框的值發送id值嗎?

$(document).ready(function() { 
    $("#button").click(function() { 

     var _t1 = $("#t1").val(); 
     var _t2 = $("#t2").val();//users write values ​​like 'A2' 'B8' here 
     //alert("Values:" + _t1 + ' ' + _t2); 

     $(document).ready(function() { 
      var value = $("#B5"); //I need to send TextBox's value instead of 
      writing like this 
      var offset = value.offset(); 
      //alert("x : " + offset.top + " y : " + offset.left); 

      <tr> 
       <td>5</td> 
       <td class="d" id="A5"></td> 
       <td class="w" id="B5"></td> 
       <td class="d" id="C5"></td> 
       <td class="w" id="D5"></td> 
       <td class="d" id="E5"></td> 
       <td class="w" id="F5"></td> 
       <td class="d" id="G5"></td> 
       <td class="w" id="H5"></td> 
      </tr> 
+1

在哪裏文本框? –

+0

我沒有發送所有的代碼。它不需要這個狀態。 –

回答

1

假設t2是你的文本框,你會想是這樣的:

var td = $('#' + $('#t2').val());

這樣做是它的文本框的值追加到「#」字符,然後通過這個jQuery作爲選擇器的連接字符串。

從上面使用的符號:

$(document).ready(function() { 
 
    $("#button").click(function() { 
 
    var inputtedID = $("#t2").val(); 
 
    if (inputtedID) { 
 
     //check that it is not an empty string 
 

 
     var td = $("#" + inputtedID); 
 
     if (td.length > 0) { 
 
     //if there is a td with that ID 
 
     var offset = td.offset(); 
 
     $("#result").text("The offset for " + inputtedID + ": {top: " + offset.top + ", left: " + offset.left + "}"); 
 
     } else { 
 
     $("#result").text("There is no element with id of " + inputtedID); 
 
     } 
 
    } 
 
    }); 
 
});
td{ 
 
    min-width: 10px; 
 
    min-height: 10px; 
 
    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td>5</td> 
 
    <td class="d" id="A5"></td> 
 
    <td class="w" id="B5"></td> 
 
    <td class="d" id="C5"></td> 
 
    <td class="w" id="D5"></td> 
 
    <td class="d" id="E5"></td> 
 
    <td class="w" id="F5"></td> 
 
    <td class="d" id="G5"></td> 
 
    <td class="w" id="H5"></td> 
 
    </tr> 
 
</table> 
 

 
<input id="t2" /> 
 

 
<button id="button">Get Offset</button> 
 
<br> 
 
<span id="result"></span>

+0

非常感謝。我可以多問一個問題嗎?我想在td下選擇圖片。意思是,我怎樣才能選擇想要在這個代碼中的圖像(var td = $(「#」+ inputsID);)只有一個圖像和圖像沒有id。 –

+0

很高興幫助。如果這解決了您的問題,請考慮將答案標記爲已接受。至於你的其他問題,如果圖像在'td'元素內,並且只有一個圖像,那麼你必須寫'var image = td.find(「img」);'。你可以在這裏閱讀'.find()':https://api.jquery.com/find/ – Botimoo

+0

Thank youuuuu :) –

相關問題