2011-06-05 83 views
1

任何人都可能遇到原型挑戰?我是Prototype的新手,我正在努力做到以下幾點。會在jQuery中做到這一點,但唯一安裝的js庫是Prototype。也許有人有一個JavaScript的解決方案,將工作?使用原型或Javascript選擇和隱藏元素

  1. 地帶任何空白的表格值的(之前和之後)
  2. 如果輸入形式長度爲3個或隱藏所有以下所有行中的checkPrice.gif圖像。

不確定這是否可以使用Prototype。

<form method="get" id="searchForm" name="car" action="some-action"> 
<input type="text" value="Ford150" name="carPart" id="search" class="textContent"> 
</form> 

<table border="1"> 
<tr> 
<td class="description">Description:</td> 
<td class="checkPrice"><p>Type:</p> 
<p><a target="_blank" href="link.html"><img src="images/checkPrice.gif"></a></p> 
</td> 
</tr> 

<tr> 
<td class="description">Description:</td> 
<td class="checkPrice"><p>Type:</p> 
<p><a target="_blank" href="link.html"><img src="images/checkPrice.gif"></a></p> 
</td> 
</tr> 
</table> 
  • 行重複

感謝這麼多的人誰可以幫助!

回答

2

我假設你的意思是「如果輸入表格值長度是3或更少」,因爲默認輸入值不是一個數字。

function updateCheckPrice(event) { 
    // $F() returns a string 
    // String.strip() trims whitespace and returns a new string 
    // String.length is a native property 
    var length = $F(this).strip().length; 

    // If length is 3 or less... 
    var action = length <= 3 ? Element.hide : Element.show; 

    // Pass the chosen show/hide function to every img element 
    $$('img[src$=checkPrice.gif]').each(action); 
} 

document.observe('dom:loaded', function(){ 
    // Update as you type 
    Event.observe('search', 'keyup', updateCheckPrice); 
}); 
+0

謝謝clockworkgeek!對不起,我的意思是如果長度是3或更少。 – chainwork 2011-06-06 21:09:40

+0

出於某種原因,updateCheckPrice似乎不會觸發。它應該會在dom加載後啓動嗎? – chainwork 2011-06-06 22:04:22

+0

函數'updateCheckPrice'應該在輸入字段的每個按鍵上觸發。它被封裝在'dom:loaded'事件中,以確保在試圖觀察它之前該字段已存在。我還沒有測試過。 – clockworkgeek 2011-06-06 22:13:32

1

好的。修剪功能用純JS(從http://blog.stevenlevithan.com/archives/faster-trim-javascript):

function trim (str) { 
var str = str.replace(/^\s\s*/, ''), 
    ws = /\s/, 
    i = str.length; 
while (ws.test(str.charAt(--i))); 
return str.slice(0, i + 1); 
} 

如何將字符串轉換爲數字:http://www.javascripter.net/faq/convert2.htm

如何訪問元素的值用原型: http://www.prototypejs.org/api/form/element/getValue

如何檢查是可變的 - 字符串:

function is_string(input){ 
    return typeof(input)=='string'; 
} 

如何使用原型隱藏元素: http://www.prototypejs.org/api/element/hide

現在您已準備好自己解決任務! ;)