2013-04-05 46 views
1

嗨我想製作一個MVC應用程序,可以在文本框中輸入隨機文本。 我做這個簡單的腳本從陣列得到一個隨機字在文本框中插入隨機文本cshtml javascript MVC3

<script type="text/javascript"> 
function random() { 
    var adjective = new Array('quick', 'pleasant', 'successive', 'interim', 
    'optimistic', 'sensitive', 'temporary', 'reminiscent', 'drooly', 'muddy', 
    'maddening', 'ceaseless', 'bawdy', 'earthy', 'european', 'aboard', 
    'innovative', 'broad', 'smelly', 'mad', 'reduced', 'plucky', 'glib', 'liable', 
    'ratty', 'venomous', 'chivalrous', 'wide', 'moral', 'scary'); 
    rdm = Math.floor(Math.random() * adjective.length); 
    document.getElementById("adj1").nodeValue = adjective[rdm]; 
} 

,然後我有類似

`

<div class="editor-label"> 
    @Html.LabelFor(model => model.adj1) 
    <button type="button" title="Random" id="1" onclick="random()"> 
     <img src="../../Content/themes/base/images/random.png" alt="" /> 
    </button> 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.adj1) 
    @Html.ValidationMessageFor(model => model.adj1) 
</div>` 

其看來,我的代碼永遠不會到達隨機函數,我不知道我在這裏做錯了什麼。

+0

你確定'隨機()'函數沒有運行?通過在函數的開頭添加一個alert('runs');'或類似的東西來測試它。 – 2013-04-05 06:55:59

+0

我試過了,沒有信息:S – laitha0 2013-04-05 06:58:44

回答

0

試試這個:

document.getElementById("adj1").value = adjective[rdm]; 
+0

同樣的輸出,沒有發生點擊 – laitha0 2013-04-05 06:57:24

+0

你的解決方案修復,不知道爲什麼它沒有在開始,但現在它的工作原理。謝謝 – laitha0 2013-04-05 07:05:06

+0

@ user2247823歡迎您:) – karaxuna 2013-04-05 07:14:24