2012-04-20 190 views
0
<script> 
    $(document).ready(function() { 
    $("#pid1").click(function() { 
     $(this).hide(SpeedEnteredByUser); 
    }); 
}); 
</script> 

<body> 
<p id = "pid1">This is paragraph</p> 
</body> 

我想在點擊段落時調用jquery hide函數。但我想通過用戶輸入的速度(即慢/快)。如何實現? 這裏SpeedEnteredByUser是用戶輸入的值(使用某種形式),我想通過這個值來隱藏函數。將參數傳遞給jquery函數

+0

用戶如何選擇速度?這是一個輸入嗎?或選擇?用戶輸入了一個數字嗎?到「慢/快」等字樣? – ManseUK 2012-04-20 09:13:59

回答

2

使用.val() jQuery的方法來獲取文本輸入的值(應該保持你的速度值!)

$(this).hide(parseInt($("#hide_speed").val(),10)); 

parseInt()用於確保輸入值確實是一個數字。該函數接受2個參數,值本身和一個基數值(10表示十進制數,2表示二進制,16表示六進制等)。

也有可能與<select> S和其他類型的輸入做。

0

解決您的ID選擇,並通過VAL表單元素的使用價值()函數

<script> 
     $(document).ready(function() { 
     $("#pid1").click(function() { 
      $(this).hide(parseInt($("#youtextinput").val(),10)); 
     }); 
    }); 
    </script> 

    <body> 
    <p id = "pid1">This is paragraph</p> 
    <input type="text" id="youtextinput"/> 
    </body> 
+0

請用小數。 – 2012-04-20 09:10:40

0

你的問題是相當模糊......但在這裏不用...爲使用戶能夠選擇的速度添加到您的HTML:

<select id="UserSelectedSpeed"> 
    <option value="slow">Slow</option> 
    <option value="fast">Fast</option> 
</select> 

然後執行以下操作:

$(document).ready(function() { 
    $("#pid1").click(function() { 
     $(this).hide($('#UserSelectedSpeed').val()); 
    }); 
}); 

$('#UserSelectedSpeed').val()select列表中獲取當前選定的值。

Working example here