带前缀的 jquery 选择器
这是我的 HTML 代码:
<div id="editorRows">
<div class="editorRow">
<input type="hidden" value="01c4ed6d-1234-4951-b048-d86208636479" autocomplete="off" name="comds.index">
Grupa:
<select id="comds_01c4ed6d-1234-4951-b048-d86208636479__Grupa" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Grupa">
Produsul:
<select id="comds_01c4ed6d-1234-4951-b048-d86208636479__Produs" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Produs">
Cantitate:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Cantitate" type="text" value="0" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Cantitate" data-val-required="The Cantitate field is required." data-val-number="The field Cantitate must be a number." data-val="true">
Pret:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Pret" type="text" value="17.23" size="4" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Pret" data-val-required="The Pret field is required." data-val-number="The field Pret must be a number." data-val="true">
TVA:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__TVA" type="text" value="0.24" name="comds[01c4ed6d-1234-4951-b048-d86208636479].TVA" data-val-required="The TVA field is required." data-val-number="The field TVA must be a number." data-val="true">
Total:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Total" type="text" value="0" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Total" data-val-required="The Total field is required." data-val-number="The field Total must be a number." data-val="true">
<a class="deleteRow" href="#">Sterge</a>
</div>
<div class="editorRow">
<input type="hidden" value="97b4ac65-73f8-4339-a707-bad53763fb2e" autocomplete="off" name="comds.index">
Grupa:
<select id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Grupa" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Grupa">
Produsul:
<select id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Produs" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Produs">
Cantitate:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Cantitate" type="text" value="0" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Cantitate">
Pret:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Pret" type="text" value="17.23" size="4" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Pret">
TVA:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__TVA" type="text" value="0.24" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].TVA">
Total:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Total" type="text" value="0" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Total">
<a class="deleteRow" href="#">Sterge</a>
</div>
<div class="editorRow">.....
</div>
因此,我正在渲染部分视图以显示更多项目,并且我有一个操作来创建更多项目。 当我更改第一个 DropDownList 中的值以重新填充第二个 DropDownList 时,我尝试调用 Json 函数。 这是我的脚本:更新
<script type="text/javascript">
$(document).ready(function () {
$('name$=.Grupa').change(function () {
var url = '<%= Url.Content("~/") %>' + "Comenzi/ForProduse";
var ddlsource = $(this);
var ddltarget = $(this).siblings('[name$=.Produs]:first');
$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
$(ddltarget).empty();
$.each(data, function (index, optionData) {
$(ddltarget).append("<option value='" + optionData.Value + "'>" + optionData.Text + "</option>");
});
});
});
});
</script>
来自控制器的我的 Json 操作未被调用。我必须在脚本中更改哪些内容才能正常工作?谢谢!
This is my HTML code:
<div id="editorRows">
<div class="editorRow">
<input type="hidden" value="01c4ed6d-1234-4951-b048-d86208636479" autocomplete="off" name="comds.index">
Grupa:
<select id="comds_01c4ed6d-1234-4951-b048-d86208636479__Grupa" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Grupa">
Produsul:
<select id="comds_01c4ed6d-1234-4951-b048-d86208636479__Produs" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Produs">
Cantitate:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Cantitate" type="text" value="0" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Cantitate" data-val-required="The Cantitate field is required." data-val-number="The field Cantitate must be a number." data-val="true">
Pret:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Pret" type="text" value="17.23" size="4" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Pret" data-val-required="The Pret field is required." data-val-number="The field Pret must be a number." data-val="true">
TVA:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__TVA" type="text" value="0.24" name="comds[01c4ed6d-1234-4951-b048-d86208636479].TVA" data-val-required="The TVA field is required." data-val-number="The field TVA must be a number." data-val="true">
Total:
<input id="comds_01c4ed6d-1234-4951-b048-d86208636479__Total" type="text" value="0" name="comds[01c4ed6d-1234-4951-b048-d86208636479].Total" data-val-required="The Total field is required." data-val-number="The field Total must be a number." data-val="true">
<a class="deleteRow" href="#">Sterge</a>
</div>
<div class="editorRow">
<input type="hidden" value="97b4ac65-73f8-4339-a707-bad53763fb2e" autocomplete="off" name="comds.index">
Grupa:
<select id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Grupa" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Grupa">
Produsul:
<select id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Produs" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Produs">
Cantitate:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Cantitate" type="text" value="0" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Cantitate">
Pret:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Pret" type="text" value="17.23" size="4" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Pret">
TVA:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__TVA" type="text" value="0.24" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].TVA">
Total:
<input id="comds_97b4ac65-73f8-4339-a707-bad53763fb2e__Total" type="text" value="0" name="comds[97b4ac65-73f8-4339-a707-bad53763fb2e].Total">
<a class="deleteRow" href="#">Sterge</a>
</div>
<div class="editorRow">.....
</div>
So, I'm Rendering a partial view to display more items and I have an action to create more.
I'm trying to call a Json function when I change the value in the first DropDownList to repopulate the second one.
This is my script: UPDATE
<script type="text/javascript">
$(document).ready(function () {
$('name$=.Grupa').change(function () {
var url = '<%= Url.Content("~/") %>' + "Comenzi/ForProduse";
var ddlsource = $(this);
var ddltarget = $(this).siblings('[name$=.Produs]:first');
$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
$(ddltarget).empty();
$.each(data, function (index, optionData) {
$(ddltarget).append("<option value='" + optionData.Value + "'>" + optionData.Text + "</option>");
});
});
});
});
</script>
My Json Action from the controller is not being called. What must I change in the script in order to work? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是唯一的问题,但我认为您正在构建的选择器不是您想要制作的。
将与类 editRow 和类 .Grupa 匹配一个元素
,我认为这一行:
可能应该是
$(this)
;你的 var ddlsource = '.editRow.Grupa';
应该类似于 var target = $(this).siblings('[name$=.Produs]:first');< /code>
然后其中任何一个:
$(ddltarget)
应该改为目标。像这样的事情:
我还没有测试过这个,但这就是你需要做的事情的总体思路。请注意,我使用的选择器效率很低,如果您可以更改生成的代码,以便您的选择框具有已知的类,那么代码会更整洁,也更有效
not sure this is the only problem, but i think the selectors you are building are not what you mean to make.
will match an element with the class editRow and the class .Grupa
i think this line:
should probably just be
$(this)
;and your
var ddlsource = '.editRow.Grupa';
should be something like
var target = $(this).siblings('[name$=.Produs]:first');
and then any of these:
$(ddltarget)
should be target instead.soemthing like this:
i haven't tested this, but that's the general idea of what you need to go for. note, the selectors i'm using are quite inefficient, if you can change the generated code so that your select boxes have a known class, it'd be much tidier code, and more effective
除非我只是错过了
$('.editRow.Grupa')
似乎与您的 html 中的任何内容都不匹配。这表示要查找具有editRow
和Grupa
类的所有元素。您没有属于Grupa
类的元素。请参阅此选择器列表以了解选择项目的有效方法:
http://api.jquery.com/category/选择器/
如果您将其更改为
$('.editRow .Grupa')
并向您的 Grupa 选择添加一个类,即Grupa
,那么您的选择器就关闭了。这意味着,获取类为
editRow
且类为Grupa
的元素内部的所有元素。您的选择器表示您需要所有同时具有editRow
和Grupa
类的元素。Unless I'm just missing it
$('.editRow.Grupa')
doesn't appear to match anything in your html. This says to find all elements with a class ofeditRow
andGrupa
. You have no elements with a class ofGrupa
.See this list of selectors for valid ways of selecting items:
http://api.jquery.com/category/selectors/
Your selector is close if you change it to
$('.editRow .Grupa')
and add a class to your Grupa select that isGrupa
.What this is saying is, get all elements which are inside of an element with the class of
editRow
and have the class ofGrupa
. Your selector says that you want all element which have both the classeditRow
andGrupa
.