Jquery - 更改动态下拉所选元素
我有一个动态生成的下拉菜单,我需要使用 Jquery 更改所选值。
<select class="txtfield country" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country">
<option value="FJ">FIDJI</option>
<option value="FI">FINLANDE</option>
<option value="FR" selected="selected">FRANCE METROPOLITAINE</option>
<option value="GA">GABON</option>
</select>
一种方法是使用下拉列表的整个 ID(包括 ctl00..):
$j("#ctl00_MainContentAreaPlaceHolder_DeliveryPersonalInformation_country option[value='FR']").attr('selected', 'selected');
有没有一种方法可以使用 CSS 找到该元素并更改值,因为我不喜欢使用动态控件的 ID?
编辑:
我忘了提及,页面上有 2 个具有相同下拉名称的自定义控件。
所以自定义控件 1 生成:
<select class="txtfield ckgcountry" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country"> ...
<option value="ZW">ZIMBABWE</option>
</select>
客户控件 2 生成:
<select class="txtfield country" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country">
<option value="FJ">FIDJI</option>
<option value="FI">FINLANDE</option>
<option value="FR" selected="selected">FRANCE METROPOLITAINE</option>
<option value="GA">GABON</option>
</select>
因此,使用代码,它仅更改在 DOM 中找到的第一个名称的值,我如何更改第二个名称的值...有办法吗这使用 CSS 吗?
I have a dynamically generated Dropdown for which I need to change the selected value using Jquery.
<select class="txtfield country" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country">
<option value="FJ">FIDJI</option>
<option value="FI">FINLANDE</option>
<option value="FR" selected="selected">FRANCE METROPOLITAINE</option>
<option value="GA">GABON</option>
</select>
One way is to use the dropdown's entire ID (includng ctl00..) :
$j("#ctl00_MainContentAreaPlaceHolder_DeliveryPersonalInformation_country option[value='FR']").attr('selected', 'selected');
Is there a way using CSS I can find the element and change the value since I do not prefer using the dynamic control's ID?
EDIT :
I forgot to mention that I have 2 custom controls on the page with the same dropdown name.
so custom control 1 generates:
<select class="txtfield ckgcountry" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country"> ...
<option value="ZW">ZIMBABWE</option>
</select>
and customer Control 2 generates:
<select class="txtfield country" id="ctl00_MainContentAreaPlaceHolder_personalInformation_country" name="ctl00$MainContentAreaPlaceHolder$personalInformation$country">
<option value="FJ">FIDJI</option>
<option value="FI">FINLANDE</option>
<option value="FR" selected="selected">FRANCE METROPOLITAINE</option>
<option value="GA">GABON</option>
</select>
So using the code it changes the value of only the first name it finds in the DOM, how do i change the value of the second one...is there a way to do this using CSS ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在使用 ASP.NET。
使用完整的 ASP.NET 控件 ID(渲染后)确实不可靠,因为它可能在某些时候发生变化。 ID 中唯一保证不更改的部分是在 ASP.NET 控件的 ID 属性中定义的 ID 的最后部分。
使用通配符选择器选择该项目
,或者
$='personalInformation_country'
将匹配所有以“personalInformation_country”结尾的 ID,该 ID 只能是一个!Looks like you are using ASP.NET.
It really isn't reliable to use the full ASP.NET control ID (post-render) because it could change at some point. The only portion of the ID that is guaranteed not to change is the last part of the ID which was defined in the ASP.NET Control's ID property.
Select that item with a wildcard selector
or
The
$='personalInformation_country'
will match all ID's ending with "personalInformation_country", which should only be one!你的意思是这样吗?
这将为所有具有 CSS 类“country”的选择元素设置选择。
如果您想更好地定位选择,则需要使用 ID/名称或确保它以您可以确定的方式嵌套。位于。
例如
Do you mean like this?
This will set the selection for all select elements with the CSS class "country"
If you want to pinpoint the select better, you'll need to use the ID/Name or ensure it is nested in a way you can be sure it can be located.
e.g.
如果没有看到页面上的其余标记,很难判断,但是您需要确保唯一性,以便获得正确的选择元素,因此 id 通常是最佳选择。
如果您可以使用确切的 ID,因为这具有最佳性能,但是您要求选项,所以就这样了。
几个选项:
$('select.txtfield')
将查找具有 txtfield 类的所有选择,因此可能不是唯一的。更好的选择可能是
$('select.txtfield.country')
我认为它是独一无二的?或者网络表单的“黑客”是使用结尾选择器
It is hard to tell without seeing the rest of the markup on the page however you need to ensure uniquness so you get the correct select element so the id is usually the best bet.
If you can use the exact id as this has the best perfrmance however you asked for options so here it goes.
A few options:
$('select.txtfield')
will find all selects with the class txtfield therefore may not be unique.A better bet maybe
$('select.txtfield.country')
which I assume is unique?or a 'hack' for webforms is to use the ends-with selector