剃须刀页面设置输入值在按钮上单击

发布于 2025-02-07 19:42:52 字数 1172 浏览 2 评论 0 原文

我想单击剃须刀页面上的一个按钮,该按钮为“客户端上的两个输入字段”设置值。

我有两个输入字段

<input id="pageBatteryName" name="pageBatteryName" asp-for="Firmware.BatteryName" class="form-control"  />
<select id="pageDeviceId" name="pageDeviceId" asp-for="Firmware.DeviceId" class ="form-control" asp-items="ViewBag.DeviceId"></select>

一个按钮

<input type="button" onClick="AutoFill()" value="Auto Fill"/>

和一个JavaScript

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script type="text/javascript">
    function AutoFill()
    {
        var vBatteryName = document.getElementsByName('pageBatteryName');
        vBatteryName.value = "QWERTY1234";
        var vDeviceId = document.getElementsByName('pageDeviceId');
        vDeviceId.value = 2;
        vBatteryName.dispatchEvent(new Event('change'));
        vDeviceId.dispatchEvent(new Event('change'));

    }
</script>
}

上面的代码不起作用。我花一天的时间尝试这样做。你能帮我吗?

如果按钮可行,我将基于选定的输入文件名(几乎所有的输入字段)设置输入。 我使用了OnPostautofill Post C#方法,但它重新加载了一个页面,发现JavaScript在这里很有用。如果JavaScript不适合此任务,您可以向我展示方式吗?

I want to click a button on a razor page that set values for two input fields "on the client side".

I have two input fields

<input id="pageBatteryName" name="pageBatteryName" asp-for="Firmware.BatteryName" class="form-control"  />
<select id="pageDeviceId" name="pageDeviceId" asp-for="Firmware.DeviceId" class ="form-control" asp-items="ViewBag.DeviceId"></select>

A button

<input type="button" onClick="AutoFill()" value="Auto Fill"/>

and a javascript

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script type="text/javascript">
    function AutoFill()
    {
        var vBatteryName = document.getElementsByName('pageBatteryName');
        vBatteryName.value = "QWERTY1234";
        var vDeviceId = document.getElementsByName('pageDeviceId');
        vDeviceId.value = 2;
        vBatteryName.dispatchEvent(new Event('change'));
        vDeviceId.dispatchEvent(new Event('change'));

    }
</script>
}

The code above do not work. I spend a day trying to do that. Could You help me please?

If the button works I will set the inputs based on selected input file name (nearly all of input fields for my form).
I used OnPostAutoFill post c# method but it reloads a page and I found that javascript can be usefull here. If javascript is not suitable for this task, can you show me the way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-02-14 19:42:52

getElementsbyname 返回元素数组。相反,您可以使用 getElementById 获取单个元素:

var vBatteryName = document.getElementById('pageBatteryName');
vBatteryName.value = "QWERTY1234";

这适用于输入字段。

对于选择,您需要将选项元素添加到这样的选择:

var opt = document.createElement('option');
opt.value = 0;
opt.innerHTML = 1;
vDeviceId.appendChild(opt);
    

请参阅此jsfiddle以查看您的代码:

getElementsByName returns an array of elements. Instead, you can use getElementById to get a single element:

var vBatteryName = document.getElementById('pageBatteryName');
vBatteryName.value = "QWERTY1234";

This works for an input field.

For a select, you want to add option elements to the select like this:

var opt = document.createElement('option');
opt.value = 0;
opt.innerHTML = 1;
vDeviceId.appendChild(opt);
    

See this jsfiddle to see your code in action: https://jsfiddle.net/12xndyLo/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文