我是JavaScript和jQuery的新手。我想显示一个combobox-A,这是一个HTML <选择>与其选择的id和内容在onChange()的其他地方。
如何通过其选择id完整的组合框,以及如何通过onChange事件的其他参数?
我是JavaScript和jQuery的新手。我想显示一个combobox-A,这是一个HTML <选择>与其选择的id和内容在onChange()的其他地方。
如何通过其选择id完整的组合框,以及如何通过onChange事件的其他参数?
当前回答
这为我onchange = setLocation($(This).val())
在这里。
@Html.DropDownList("Demo",
new SelectList(ViewBag.locs, "Value", "Text"),
new { Class = "ddlStyle", onchange = "setLocation($(this).val())" });
其他回答
这对我有帮助。
选择:
$('select_tags').on('change', function() {
alert( $(this).find(":selected").val() );
});
For radio / checkbox:
$('radio_tags').on('change', function() {
alert( $(this).find(":checked").val() );
});
我发现@Piyush的回答很有帮助,只是补充一下,如果您以编程方式创建一个选择,那么有一个重要的方法来获得这种行为,可能不明显。假设你有一个函数,你创建了一个新的select:
var changeitem = function (sel) {
console.log(sel.selectedIndex);
}
var newSelect = document.createElement('select');
newSelect.id = 'newselect';
正常的行为可能是说
newSelect.onchange = changeitem;
但是这并不允许你指定传入的参数,所以你可以这样做:
newSelect.setAttribute('onchange', 'changeitem(this)');
你可以设置参数。如果采用第一种方法,那么onchange函数的参数将依赖于浏览器。第二种方法似乎在跨浏览器中工作得很好。
这为我onchange = setLocation($(This).val())
在这里。
@Html.DropDownList("Demo",
new SelectList(ViewBag.locs, "Value", "Text"),
new { Class = "ddlStyle", onchange = "setLocation($(this).val())" });
您可以尝试咆哮代码
<select onchange="myfunction($(this).val())" id="myId">
</select>
函数setMyValue(v) { console.log (v); } <选择onchange = " setMyValue (this.value)" > <选项值= " " > 1 > < /选项 <选项值= " b " > 2 > < /选项 <选项值= " c " > 3 < /选项> < /选择>