1

您好我正在尝试制作一个可以在文本框中输入随机文本的 MVC 应用程序。我制作了这个简单的脚本来从数组中获取随机单词

<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>`

似乎我的代码永远不会到达随机函数,我不确定我在这里做错了什么。

4

1 回答 1

0

试试这个:

document.getElementById("adj1").value = adjective[rdm];
于 2013-04-05T06:53:08.970 回答