【问题标题】:How to trigger jquery ".change" event on dropdown select programatically c#如何以编程方式在下拉选择中触发jquery“.change”事件c#
【发布时间】:2017-08-24 12:38:39
【问题描述】:

我正在尝试使用 c# 自动化 html 事件。
我有 html 下拉菜单,并在更改其值时显示/隐藏特定的 div。
下面是示例 HTML 代码。

<!DOCTYPE html>
<html>
<body>
<select id="ddl">
    <option value="1" selected="selected">TextBox</option>
    <option value="2">Button</option>
  </select>

<input style="display: none" type="text" class="textboxclass" />

<div class="btn" style="display: none">
    <button type="button">Submit</button>
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$('#ddl').change(function()
{var name = $('#ddl option:selected').text();
var id = $('#ddl option:selected').val();
if(id =="1"){$('.textboxclass').show();$('.btn').hide();}
else{$('.textboxclass').hide();$('.btn').show();}});
</script>

</body>
</html>

我正在使用 mshtml.dll 来读取 HtmlDocument,我必须以编程方式触发“更改”事件。 尝试了以下不同的方法:

HTMLOptionElement drpelem.select=true;
HTMLSelectElement elem.click();


但它不起作用。请指导我。

【问题讨论】:

  • 你可以使用jQuery来执行更改事件,比如$('#ddl').trigger('change')$('#ddl').change()
  • 我需要通过c#代码触发事件。我尝试使用 .fireevent("onchange") 它适用于 javascript 调用。但同样不适用于 jquery $().change() 事件。

标签: c# jquery html mshtml


【解决方案1】:

我认为这个问题已经以不同的形式提出和回答,这里:https://stackoverflow.com/a/1456714/19020 或这里:https://stackoverflow.com/a/11175179/19020——因为 Internet Explorer 使用 mshtml。

但实际上,要加载完整的浏览器体验,您需要加载 ActiveX 控件、COM 控件或 Microsoft 现在所称的任何内容。据我所知,mshtml.dll 只是呈现 HTML。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多